Skip to content

Commit

Permalink
(third) Convert examples into test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Aug 20, 2021
1 parent 4e83e93 commit 3cb74f1
Show file tree
Hide file tree
Showing 9 changed files with 8,881 additions and 152 deletions.
1 change: 1 addition & 0 deletions experiments/stasm/third/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.nyc_output
*.js
13 changes: 5 additions & 8 deletions experiments/stasm/third/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,16 @@ Easy. Variable references are first-class citizens of the data model, making it
- Restricting message references and introducing phrases
- [#149 Restricting message references](https://github.com/unicode-org/message-format-wg/discussions/149)

## How to Run
## Running the Test Suite

Run all examples:

npm start
npm test

Run each example individually:
Run an example individually:

npm run example glossary
npm run example phrases
npm run example list
npm run example number
npm run example opaque
npx tsc
node example/example_number.js

## References

Expand Down
218 changes: 149 additions & 69 deletions experiments/stasm/third/example/example_glossary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {test} from "tap";
import {FormattingContext} from "../impl/context.js";
import {Argument, Message, Parameter} from "../impl/model.js";
import {REGISTRY} from "../impl/registry.js";
Expand Down Expand Up @@ -104,9 +105,7 @@ REGISTRY["ACTOR"] = function get_noun(
}
};

console.log("==== English ====");

{
test("NOUN is ADJECTIVE (English)", (tap) => {
let message: Message = {
lang: "en",
id: "accord",
Expand Down Expand Up @@ -135,159 +134,227 @@ console.log("==== English ====");
},
],
};
console.log(

tap.equal(
formatMessage(message, {
item: new StringValue("t-shirt"),
color: new StringValue("red"),
})
}),
"The T-shirt is red."
);

console.log(
Array.of(
...formatToParts(message, {
item: new StringValue("t-shirt"),
color: new StringValue("red"),
})
)
tap.same(
formatToParts(message, {
item: new StringValue("t-shirt"),
color: new StringValue("red"),
}),
[
{type: "literal", value: "The "},
{type: "literal", value: "T-shirt"},
{type: "literal", value: " is "},
{type: "literal", value: "red"},
{type: "literal", value: "."},
]
);
}

{
tap.end();
});

test("NOUN is ADJECTIVE (Polish; requires according the gender of the adjective)", (tap) => {
let message: Message = {
lang: "en",
id: "you-see",
lang: "pl",
id: "accord",
phrases: {},
selectors: [],
variants: [
{
keys: [],
value: [
{type: "StringLiteral", value: "You see "},
{
type: "FunctionCall",
name: "ACTOR",
args: [{type: "VariableReference", name: "monster"}],
name: "NOUN",
args: [{type: "VariableReference", name: "item"}],
opts: {
INDEFINITE: {type: "BooleanLiteral", value: true},
CAPITALIZED: {type: "BooleanLiteral", value: true},
},
},
{type: "StringLiteral", value: "!"},
{type: "StringLiteral", value: " jest "},
{
type: "FunctionCall",
name: "ADJECTIVE",
args: [{type: "VariableReference", name: "color"}],
opts: {
ACCORD_WITH: {type: "VariableReference", name: "item"},
},
},
{type: "StringLiteral", value: "."},
],
},
],
};
console.log(

tap.equal(
formatMessage(message, {
monster: new StringValue("dinosaur"),
})
item: new StringValue("t-shirt"),
color: new StringValue("red"),
}),
"Tiszert jest czerwony."
);
}

{
tap.same(
formatToParts(message, {
item: new StringValue("t-shirt"),
color: new StringValue("red"),
}),
[
{type: "literal", value: "Tiszert"},
{type: "literal", value: " jest "},
{type: "literal", value: "czerwony"},
{type: "literal", value: "."},
]
);

tap.end();
});

test("Subject verb OBJECT (English)", (tap) => {
let message: Message = {
lang: "en",
id: "they-wave",
id: "you-see",
phrases: {},
selectors: [],
variants: [
{
keys: [],
value: [
{type: "StringLiteral", value: "You see "},
{
type: "FunctionCall",
name: "ACTOR",
args: [{type: "VariableReference", name: "monster"}],
opts: {
DEFINITE: {type: "BooleanLiteral", value: true},
CAPITALIZED: {type: "BooleanLiteral", value: true},
INDEFINITE: {type: "BooleanLiteral", value: true},
},
},
{type: "StringLiteral", value: " waves at you!"},
{type: "StringLiteral", value: "!"},
],
},
],
};
console.log(

tap.equal(
formatMessage(message, {
monster: new StringValue("ogre"),
})
monster: new StringValue("dinosaur"),
}),
"You see a dinosaur!"
);

tap.same(
formatToParts(message, {
monster: new StringValue("dinosaur"),
}),
[
{type: "literal", value: "You see "},
{type: "literal", value: "a dinosaur"},
{type: "literal", value: "!"},
]
);
}

console.log("==== polski ====");
tap.end();
});

{
test("Subject verb OBJECT (Polish; requires the accusative case)", (tap) => {
let message: Message = {
lang: "pl",
id: "accord",
id: "you-see",
phrases: {},
selectors: [],
variants: [
{
keys: [],
value: [
{type: "StringLiteral", value: "Widzisz "},
{
type: "FunctionCall",
name: "NOUN",
args: [{type: "VariableReference", name: "item"}],
opts: {
CAPITALIZED: {type: "BooleanLiteral", value: true},
},
},
{type: "StringLiteral", value: " jest "},
{
type: "FunctionCall",
name: "ADJECTIVE",
args: [{type: "VariableReference", name: "color"}],
name: "ACTOR",
args: [{type: "VariableReference", name: "monster"}],
opts: {
ACCORD_WITH: {type: "VariableReference", name: "item"},
CASE: {type: "StringLiteral", value: "accusative"},
},
},
{type: "StringLiteral", value: "."},
{type: "StringLiteral", value: "!"},
],
},
],
};
console.log(

tap.equal(
formatMessage(message, {
item: new StringValue("t-shirt"),
color: new StringValue("red"),
})
monster: new StringValue("dinosaur"),
}),
"Widzisz dinozaura!"
);
}

{
tap.same(
formatToParts(message, {
monster: new StringValue("dinosaur"),
}),
[
{type: "literal", value: "Widzisz "},
{type: "literal", value: "dinozaura"},
{type: "literal", value: "!"},
]
);

tap.end();
});

test("SUBJECT verb (English)", (tap) => {
let message: Message = {
lang: "pl",
id: "you-see",
lang: "en",
id: "they-wave",
phrases: {},
selectors: [],
variants: [
{
keys: [],
value: [
{type: "StringLiteral", value: "Widzisz "},
{
type: "FunctionCall",
name: "ACTOR",
args: [{type: "VariableReference", name: "monster"}],
opts: {
CASE: {type: "StringLiteral", value: "accusative"},
DEFINITE: {type: "BooleanLiteral", value: true},
CAPITALIZED: {type: "BooleanLiteral", value: true},
},
},
{type: "StringLiteral", value: "!"},
{type: "StringLiteral", value: " waves at you!"},
],
},
],
};
console.log(

tap.equal(
formatMessage(message, {
monster: new StringValue("dinosaur"),
})
monster: new StringValue("ogre"),
}),
"The ogre waves at you!"
);

tap.same(
formatToParts(message, {
monster: new StringValue("ogre"),
}),
[
{type: "literal", value: "The ogre"},
{type: "literal", value: " waves at you!"},
]
);
}

{
tap.end();
});

test("SUBJECT verb (Polish)", (tap) => {
let message: Message = {
lang: "pl",
id: "they-wave",
Expand All @@ -312,9 +379,22 @@ console.log("==== polski ====");
],
};

console.log(
tap.equal(
formatMessage(message, {
monster: new StringValue("ogre"),
})
}),
"Ogr macha do ciebie!"
);

tap.same(
formatToParts(message, {
monster: new StringValue("ogre"),
}),
[
{type: "literal", value: "Ogr"},
{type: "literal", value: " macha do ciebie!"},
]
);
}

tap.end();
});
Loading

0 comments on commit 3cb74f1

Please sign in to comment.