-
Notifications
You must be signed in to change notification settings - Fork 5
docs: added examples #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31bccf5
docs: added examples
ascandone 1721cda
chore: removed comment
ascandone bd4652b
minor: fix typo
ascandone de2d7ed
minor: better test description
ascandone 735c874
minor: fix schema
ascandone 51ffdd8
minor: moved schema key up
ascandone 91c4867
minor: moved tests into nested folder
ascandone 93905da
minor: add posting
ascandone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
internal/interpreter/testdata/numscript-cookbook/experimental/meta-calc.num
This file contains hidden or 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,10 @@ | ||
| vars { | ||
| number $x = 10 + 1 | ||
| } | ||
|
|
||
| send [USD/2 10] ( | ||
| source = @world | ||
| destination = @alice | ||
| ) | ||
|
|
||
| set_tx_meta("key", $x/2) |
16 changes: 16 additions & 0 deletions
16
internal/interpreter/testdata/numscript-cookbook/experimental/meta-calc.num.specs.json
This file contains hidden or 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,16 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "featureFlags": [ | ||
| "experimental-overdraft-function", | ||
| "experimental-mid-script-function-call", | ||
| "experimental-oneof" | ||
| ], | ||
| "testCases": [ | ||
| { | ||
| "it": "sets the expected meta", | ||
| "expect.txMetadata": { | ||
| "key": "11/2" | ||
| } | ||
| } | ||
| ] | ||
| } |
15 changes: 15 additions & 0 deletions
15
...nterpreter/testdata/numscript-cookbook/experimental/mixed-source-prefer-single-source.num
This file contains hidden or 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,15 @@ | ||
| vars { | ||
| number $amt | ||
| } | ||
|
|
||
| send [USD $amt] ( | ||
| source = oneof { | ||
| @s1 | ||
| @s2 | ||
| { | ||
| @s1 | ||
| @s2 | ||
| } | ||
| } | ||
| destination = @dest | ||
| ) |
81 changes: 81 additions & 0 deletions
81
...testdata/numscript-cookbook/experimental/mixed-source-prefer-single-source.num.specs.json
This file contains hidden or 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,81 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "variables": { | ||
| "amt": "10" | ||
| }, | ||
| "featureFlags": ["experimental-oneof"], | ||
| "testCases": [ | ||
| { | ||
| "it": "sends from the first source when there is enough balance", | ||
| "balances": { | ||
| "s1": { | ||
| "USD": 999 | ||
| } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "s1", | ||
| "destination": "dest", | ||
| "amount": 10, | ||
| "asset": "USD" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "sends from the second one when it has enough balance but the first one doesn't", | ||
| "balances": { | ||
| "s1": { | ||
| "USD": 9 | ||
| }, | ||
| "s2": { | ||
| "USD": 999 | ||
| } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "s2", | ||
| "destination": "dest", | ||
| "amount": 10, | ||
| "asset": "USD" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "sends partially from both when none of them has enough balance on its own", | ||
| "balances": { | ||
| "s1": { | ||
| "USD": 6 | ||
| }, | ||
| "s2": { | ||
| "USD": 9 | ||
| } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "s1", | ||
| "destination": "dest", | ||
| "amount": 6, | ||
| "asset": "USD" | ||
| }, | ||
| { | ||
| "source": "s2", | ||
| "destination": "dest", | ||
| "amount": 4, | ||
| "asset": "USD" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "fails if there aren't enough funds between all sources", | ||
| "balances": { | ||
| "s1": { | ||
| "USD": 5 | ||
| }, | ||
| "s2": { | ||
| "USD": 4 | ||
| } | ||
| }, | ||
| "expect.missingFunds": true | ||
| } | ||
| ] | ||
| } |
13 changes: 13 additions & 0 deletions
13
internal/interpreter/testdata/numscript-cookbook/experimental/top-up-many.num
This file contains hidden or 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,13 @@ | ||
| vars { | ||
| number $amt | ||
| } | ||
|
|
||
|
|
||
| send [EUR $amt] ( | ||
| source = @world | ||
| destination = oneof { | ||
| max overdraft(@alice, EUR) to @alice | ||
| max overdraft(@bob, EUR) to @bob | ||
| remaining kept | ||
| } | ||
| ) | ||
56 changes: 56 additions & 0 deletions
56
internal/interpreter/testdata/numscript-cookbook/experimental/top-up-many.num.specs.json
This file contains hidden or 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,56 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "featureFlags": [ | ||
| "experimental-overdraft-function", | ||
| "experimental-mid-script-function-call", | ||
| "experimental-oneof" | ||
| ], | ||
| "variables": { | ||
| "amt": "100" | ||
| }, | ||
| "testCases": [ | ||
| { | ||
| "it": "should be a noop when all balances are >= 0", | ||
| "balances": { "alice": { "EUR": 100 }, "bob": { "EUR": 200 } }, | ||
| "expect.postings": [] | ||
| }, | ||
| { | ||
| "it": "should prioritize alice when both have missing funds", | ||
| "balances": { | ||
| "alice": { "EUR": -120 }, | ||
| "bob": { "EUR": -120 } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "world", | ||
| "destination": "alice", | ||
| "amount": 100, | ||
| "asset": "EUR" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "doesn't send funds to alice if there aren't enough funds for the account to be topped-up", | ||
| "balances": { | ||
| "alice": { "EUR": -80 }, | ||
| "bob": { "EUR": -120 } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "world", | ||
| "destination": "bob", | ||
| "amount": 100, | ||
| "asset": "EUR" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "funds are kept if there are spare funds", | ||
| "balances": { | ||
| "alice": { "EUR": -10 }, | ||
| "bob": { "EUR": -20 } | ||
| }, | ||
| "expect.postings": [] | ||
| } | ||
| ] | ||
| } |
8 changes: 8 additions & 0 deletions
8
internal/interpreter/testdata/numscript-cookbook/experimental/top-up.num
This file contains hidden or 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,8 @@ | ||
| vars { | ||
| monetary $alice_overdraft = overdraft(@alice, EUR) | ||
| } | ||
|
|
||
| send $alice_overdraft ( | ||
| source = @world | ||
| destination = @alice | ||
| ) |
32 changes: 32 additions & 0 deletions
32
internal/interpreter/testdata/numscript-cookbook/experimental/top-up.num.specs.json
This file contains hidden or 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,32 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "featureFlags": ["experimental-overdraft-function"], | ||
| "testCases": [ | ||
| { | ||
| "it": "should not emit postings", | ||
| "balances": { "alice": { "EUR": 100 } }, | ||
| "expect.postings": [] | ||
| }, | ||
| { | ||
| "it": "should send the missing amount to an overdraft account", | ||
| "balances": { "alice": { "EUR": -100 } }, | ||
| "expect.volumes": { | ||
| "alice": { "EUR": 0 }, | ||
| "world": { "EUR": -100 } | ||
| }, | ||
| "expect.movements": { | ||
| "world": { | ||
| "alice": { "EUR": 100 } | ||
| } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "world", | ||
| "destination": "alice", | ||
| "amount": 100, | ||
| "asset": "EUR" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
11 changes: 11 additions & 0 deletions
11
internal/interpreter/testdata/numscript-cookbook/experimental/transfer-example.num
This file contains hidden or 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,11 @@ | ||
| vars { | ||
| number $amt | ||
| account $dest | ||
| monetary $bank_overdraft = overdraft(@bank_account, EUR) | ||
| } | ||
|
|
||
| send [EUR $amt] ( | ||
| source = max $bank_overdraft from @wallet | ||
| destination = $dest | ||
| ) | ||
|
|
45 changes: 45 additions & 0 deletions
45
...rnal/interpreter/testdata/numscript-cookbook/experimental/transfer-example.num.specs.json
This file contains hidden or 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,45 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "featureFlags": [ | ||
| "experimental-overdraft-function", | ||
| "experimental-mid-script-function-call", | ||
| "experimental-oneof" | ||
| ], | ||
| "variables": { | ||
| "amt": "100", | ||
| "dest": "alice" | ||
| }, | ||
| "testCases": [ | ||
| { | ||
| "it": "should authorize transfer if both wallet and bank accounts display enough balance", | ||
| "balances": { | ||
| "wallet": { "EUR": 100 }, | ||
| "bank_account": { "EUR": -100 } | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "wallet", | ||
| "destination": "alice", | ||
| "amount": 100, | ||
| "asset": "EUR" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "should not authorize transfer if wallet does not display enough balance and bank account does", | ||
| "balances": { | ||
| "wallet": { "EUR": 50 }, | ||
| "bank_account": { "EUR": -100 } | ||
| }, | ||
| "expect.missingFunds": true | ||
| }, | ||
| { | ||
| "it": "should not authorize transfer if bank account does not display enough balance and wallet does", | ||
| "balances": { | ||
| "wallet": { "EUR": 100 }, | ||
| "bank_account": { "EUR": -50 } | ||
| }, | ||
| "expect.missingFunds": true | ||
| } | ||
| ] | ||
| } |
16 changes: 16 additions & 0 deletions
16
internal/interpreter/testdata/numscript-cookbook/max-fee.num
This file contains hidden or 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,16 @@ | ||
| vars { | ||
| number $amt | ||
| number $cap | ||
| portion $fee | ||
| } | ||
|
|
||
| send [USD $amt] ( | ||
| source = @world | ||
| destination = { | ||
| $fee to { | ||
| max [USD $cap] to @fees | ||
| remaining to @dest | ||
| } | ||
| remaining to @dest | ||
| } | ||
| ) |
55 changes: 55 additions & 0 deletions
55
internal/interpreter/testdata/numscript-cookbook/max-fee.num.specs.json
This file contains hidden or 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,55 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json", | ||
| "variables": { | ||
| "cap": "10", | ||
| "fee": "10%" | ||
| }, | ||
| "testCases": [ | ||
| { | ||
| "it": "it sends 10% of the fee when lower than cap", | ||
| "variables": { | ||
| "amt": "10" | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "world", | ||
| "destination": "fees", | ||
| "amount": 1, | ||
| "asset": "USD" | ||
| }, | ||
| { | ||
| "source": "world", | ||
| "destination": "dest", | ||
| "amount": 9, | ||
| "asset": "USD" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "it": "it caps the fee to the $cap", | ||
| "variables": { | ||
| "amt": "500" | ||
| }, | ||
| "expect.postings": [ | ||
| { | ||
| "source": "world", | ||
| "destination": "fees", | ||
| "amount": 10, | ||
| "asset": "USD" | ||
| }, | ||
| { | ||
| "source": "world", | ||
| "destination": "dest", | ||
| "amount": 40, | ||
| "asset": "USD" | ||
| }, | ||
| { | ||
| "source": "world", | ||
| "destination": "dest", | ||
| "amount": 450, | ||
| "asset": "USD" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
11 changes: 11 additions & 0 deletions
11
internal/interpreter/testdata/numscript-cookbook/mixed-source-dest-allotment.num
This file contains hidden or 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,11 @@ | ||
| send [USD 77] ( | ||
| source = { | ||
| 10% from @src1 | ||
| 20% from @src2 | ||
| remaining from @world | ||
| } | ||
| destination = { | ||
| 1/2 to @dest1 | ||
| 1/2 to @dest2 | ||
| } | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Uninitialized variable will cause runtime error.
The variable
$amtis declared but never initialized. This will likely cause a runtime error when the script attempts to execute the send operation.Add an initialization value to fix this issue:
vars { - number $amt + number $amt = 100 }Or specify the amount directly in the send operation and remove the variable if it's meant to be a parameter.
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ascandone Got it! I understand that the uninitialized variable is intentional for this example.
🐰
╭─────╮
│ ◕ ◕ │ curious about NumScript behavior
╰─────╯
✏️ Learnings added