forked from ZenUml/core
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from mermaid-js/feat/support-number-unit-1kg
Feat/support number unit 1kg
- Loading branch information
Showing
9 changed files
with
725 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import antlr4 from "antlr4"; | ||
import { default as sequenceLexer } from "../../generated-parser/sequenceLexer"; | ||
import { default as sequenceParser } from "../../generated-parser/sequenceParser"; | ||
|
||
// Add getFormattedText to the atom context prototype | ||
sequenceParser.AtomContext.prototype.getFormattedText = function () { | ||
if (this.MONEY && this.MONEY()) { | ||
return this.MONEY().getText(); | ||
} | ||
return this.getText(); | ||
}; | ||
|
||
function parseAtom(input: string) { | ||
const chars = new antlr4.InputStream(input); | ||
const lexer = new sequenceLexer(chars); | ||
const tokens = new antlr4.CommonTokenStream(lexer); | ||
const parser = new sequenceParser(tokens); | ||
return parser.atom(); | ||
} | ||
|
||
describe("Money", () => { | ||
describe("valid cases", () => { | ||
it("should parse simple money amount and verify token", () => { | ||
const ast = parseAtom("$100"); | ||
const token = ast.MONEY().symbol; | ||
expect(ast.getFormattedText()).toBe("$100"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("MONEY"); | ||
}); | ||
|
||
it("should parse zero money amount and verify token", () => { | ||
const ast = parseAtom("$0"); | ||
const token = ast.MONEY().symbol; | ||
expect(ast.getFormattedText()).toBe("$0"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("MONEY"); | ||
}); | ||
|
||
it("should parse large money amount and verify token", () => { | ||
const ast = parseAtom("$1000000"); | ||
const token = ast.MONEY().symbol; | ||
expect(ast.getFormattedText()).toBe("$1000000"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("MONEY"); | ||
}); | ||
|
||
it("should parse money amount with leading zeros", () => { | ||
const ast = parseAtom("$01"); | ||
expect(ast.getFormattedText()).toBe("$01"); | ||
}); | ||
|
||
it("should parse decimal money amounts and verify token", () => { | ||
const ast1 = parseAtom("$1.50"); | ||
const token1 = ast1.MONEY().symbol; | ||
const ast2 = parseAtom("$0.50"); | ||
const token2 = ast2.MONEY().symbol; | ||
const ast3 = parseAtom("$.50"); | ||
const token3 = ast3.MONEY().symbol; | ||
|
||
expect(ast1.getFormattedText()).toBe("$1.50"); | ||
expect(sequenceParser.symbolicNames[token1.type]).toBe("MONEY"); | ||
expect(ast2.getFormattedText()).toBe("$0.50"); | ||
expect(sequenceParser.symbolicNames[token2.type]).toBe("MONEY"); | ||
expect(ast3.getFormattedText()).toBe("$.50"); | ||
expect(sequenceParser.symbolicNames[token3.type]).toBe("MONEY"); | ||
}); | ||
|
||
// Debug helper to print all token information | ||
it("should print token debug information", () => { | ||
const ast = parseAtom("$100"); | ||
const token = ast.MONEY().symbol; | ||
console.log("Token type:", token.type); | ||
console.log("Symbolic names:", sequenceParser.symbolicNames); | ||
console.log("Rule names:", sequenceParser.ruleNames); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import antlr4 from "antlr4"; | ||
import { default as sequenceLexer } from "../../generated-parser/sequenceLexer"; | ||
import { default as sequenceParser } from "../../generated-parser/sequenceParser"; | ||
|
||
// Add getFormattedText to the atom context prototype | ||
sequenceParser.AtomContext.prototype.getFormattedText = function () { | ||
if (this.NUMBER_UNIT && this.NUMBER_UNIT()) { | ||
return this.NUMBER_UNIT().getText(); | ||
} | ||
return this.getText(); | ||
}; | ||
|
||
function parseAtom(input: string) { | ||
const chars = new antlr4.InputStream(input); | ||
const lexer = new sequenceLexer(chars); | ||
const tokens = new antlr4.CommonTokenStream(lexer); | ||
const parser = new sequenceParser(tokens); | ||
return parser.atom(); | ||
} | ||
|
||
describe("NumberUnit", () => { | ||
describe("valid cases", () => { | ||
it("should parse simple number with unit and verify token", () => { | ||
const ast = parseAtom("1kg"); | ||
const token = ast.NUMBER_UNIT().symbol; | ||
expect(ast.getFormattedText()).toBe("1kg"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("NUMBER_UNIT"); | ||
}); | ||
|
||
it("should parse zero with unit and verify token", () => { | ||
const ast = parseAtom("0kg"); | ||
const token = ast.NUMBER_UNIT().symbol; | ||
expect(ast.getFormattedText()).toBe("0kg"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("NUMBER_UNIT"); | ||
}); | ||
|
||
it("should parse large number with unit and verify token", () => { | ||
const ast = parseAtom("100day"); | ||
const token = ast.NUMBER_UNIT().symbol; | ||
expect(ast.getFormattedText()).toBe("100day"); | ||
expect(sequenceParser.symbolicNames[token.type]).toBe("NUMBER_UNIT"); | ||
}); | ||
|
||
it("should parse multi-character units", () => { | ||
const ast1 = parseAtom("100day"); | ||
const ast2 = parseAtom("5km"); | ||
expect(ast1.getFormattedText()).toBe("100day"); | ||
expect(ast2.getFormattedText()).toBe("5km"); | ||
}); | ||
|
||
it("should parse unit without number", () => { | ||
const ast = parseAtom("kg"); | ||
expect(ast.getFormattedText()).toBe("kg"); | ||
}); | ||
|
||
it("should parse number with leading zeros", () => { | ||
const ast = parseAtom("01h"); | ||
expect(ast.getFormattedText()).toBe("01h"); | ||
}); | ||
|
||
it("should parse large number with leading zeros", () => { | ||
const ast = parseAtom("010hours"); | ||
expect(ast.getFormattedText()).toBe("010hours"); | ||
}); | ||
|
||
it("should parse decimal numbers with unit and verify token", () => { | ||
const ast1 = parseAtom("1.5kg"); | ||
const token1 = ast1.NUMBER_UNIT().symbol; | ||
const ast2 = parseAtom("0.5h"); | ||
const token2 = ast2.NUMBER_UNIT().symbol; | ||
const ast3 = parseAtom(".5m"); | ||
const token3 = ast3.NUMBER_UNIT().symbol; | ||
|
||
expect(ast1.getFormattedText()).toBe("1.5kg"); | ||
expect(sequenceParser.symbolicNames[token1.type]).toBe("NUMBER_UNIT"); | ||
expect(ast2.getFormattedText()).toBe("0.5h"); | ||
expect(sequenceParser.symbolicNames[token2.type]).toBe("NUMBER_UNIT"); | ||
expect(ast3.getFormattedText()).toBe(".5m"); | ||
expect(sequenceParser.symbolicNames[token3.type]).toBe("NUMBER_UNIT"); | ||
}); | ||
|
||
// Debug helper to print all token information | ||
it("should print token debug information", () => { | ||
const ast = parseAtom("1kg"); | ||
const token = ast.NUMBER_UNIT().symbol; | ||
console.log("Token type:", token.type); | ||
console.log("Symbolic names:", sequenceParser.symbolicNames); | ||
console.log("Rule names:", sequenceParser.ruleNames); | ||
}); | ||
}); | ||
}); |