diff --git a/src/__tests__/parser.spec.ts b/src/__tests__/parser.spec.ts index 9419b54..791e42d 100644 --- a/src/__tests__/parser.spec.ts +++ b/src/__tests__/parser.spec.ts @@ -44,6 +44,39 @@ describe('Line parser', () => { ).toThrow(); }); + it('does parse fractions with universal minus disabled', () => { + const result = parseLine_( + '3/2', + DEFAULT_NUMBER_OF_COMPONENTS, + undefined, + false, + false + ); + expect( + result.equals( + new Interval( + ExtendedMonzo.fromFraction( + new Fraction(3, 2), + DEFAULT_NUMBER_OF_COMPONENTS + ), + 'ratio' + ) + ) + ).toBeTruthy(); + }); + + it('does parse negative cents even with minus disabled for other types', () => { + const result = parseLine_( + '-1.23', + DEFAULT_NUMBER_OF_COMPONENTS, + undefined, + false, + false + ); + expect(result.type === 'cents'); + expect(result.monzo.totalCents()).toBeCloseTo(-1.23); + }); + it('rejects fractions without a numerator', () => { expect(() => parseLine('/5')).toThrow(); }); diff --git a/src/parser.ts b/src/parser.ts index 6967990..b6912fd 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -138,8 +138,10 @@ export function parseLine( universalMinus = true ): Interval { const ast = parseAst(input); - if (!universalMinus && ast.type !== 'CentsLiteral') { - throw new Error('Univeral minus violation'); + if (!universalMinus && ast.type === 'UnaryExpression') { + if (ast.operand.type !== 'CentsLiteral') { + throw new Error('Univeral minus violation'); + } } if ( !admitBareNumbers &&