From 0b43b143b105e0bbd59a07da8741963c826ebdc8 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 18 Mar 2019 10:05:24 +0700 Subject: [PATCH] add config to gst --- tests/test_config.nim | 51 ++++++++++++++++++++++++++++++++ tests/test_generalstate_json.nim | 42 ++++++++++++++++++++------ tests/test_helpers.nim | 2 +- 3 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 tests/test_config.nim diff --git a/tests/test_config.nim b/tests/test_config.nim new file mode 100644 index 000000000..9bbb8ba67 --- /dev/null +++ b/tests/test_config.nim @@ -0,0 +1,51 @@ +import + parseopt, strutils, + ../nimbus/vm/interpreter/vm_forks + +type + ConfigStatus* = enum + ## Configuration status flags + Success, ## Success + EmptyOption, ## No options in category + ErrorUnknownOption, ## Unknown option in command line found + ErrorParseOption, ## Error in parsing command line option + ErrorIncorrectOption, ## Option has incorrect value + Error ## Unspecified error + + Configuration = ref object + testSubject*: string + fork*: Fork + +var testConfig {.threadvar.}: Configuration + +proc initConfiguration(): Configuration = + result = new Configuration + result.fork = FkFrontier + +proc getConfiguration*(): Configuration {.gcsafe.} = + if isNil(testConfig): + testConfig = initConfiguration() + result = testConfig + +proc processArguments*(msg: var string): ConfigStatus = + var + opt = initOptParser() + config = getConfiguration() + + result = Success + for kind, key, value in opt.getopt(): + case kind + of cmdArgument: + config.testSubject = key + of cmdLongOption, cmdShortOption: + case key.toLowerAscii() + of "fork": config.fork = parseEnum[Fork](strip(value)) + else: + msg = "Unknown option " & key + if value.len > 0: msg = msg & " : " & value + result = ErrorUnknownOption + break + of cmdEnd: + msg = "Error processing option [" & key & "]" + result = ErrorParseOption + break diff --git a/tests/test_generalstate_json.nim b/tests/test_generalstate_json.nim index 6850b2365..c3166274d 100644 --- a/tests/test_generalstate_json.nim +++ b/tests/test_generalstate_json.nim @@ -8,8 +8,8 @@ import unittest, strformat, strutils, tables, json, ospaths, times, os, byteutils, ranges/typedranges, nimcrypto, options, - eth/[rlp, common, keys], eth/trie/[db, trie_defs], chronicles, - ./test_helpers, ../nimbus/p2p/executor, + eth/[rlp, common], eth/trie/[db, trie_defs], chronicles, + ./test_helpers, ../nimbus/p2p/executor, test_config, ../nimbus/[constants, errors, transaction], ../nimbus/[vm_state, vm_types, vm_state_transactions, utils], ../nimbus/vm/interpreter, @@ -90,7 +90,8 @@ proc testFixtureIndexes(tester: Tester, testStatusIMPL: var TestStatus) = if tester.debugMode: tester.dumpDebugData(vmState, sender, gasUsed) -proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus, debugMode = false) = +proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus, + debugMode = false, supportedForks: set[Fork] = supportedForks) = var tester: Tester var fixture: JsonNode for label, child in fixtures: @@ -110,9 +111,10 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus, debugMode = tester.debugMode = debugMode let ftrans = fixture["transaction"] + var testedInFork = false for fork in supportedForks: if fixture["post"].hasKey(forkNames[fork]): - # echo "[fork: ", forkNames[fork], "]" + testedInFork = true for expectation in fixture["post"][forkNames[fork]]: tester.expectedHash = expectation["hash"].getStr tester.expectedLogs = expectation["logs"].getStr @@ -126,6 +128,9 @@ proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus, debugMode = tester.fork = fork testFixtureIndexes(tester, testStatusIMPL) + if not testedInFork: + echo "test subject '", tester.name, "' not tested in any forks" + proc main() = if paramCount() == 0: # run all test fixtures @@ -133,9 +138,28 @@ proc main() = jsonTest("GeneralStateTests", testFixture) else: # execute single test in debug mode - let path = "tests" / "fixtures" / "GeneralStateTests" - let n = json.parseFile(path / paramStr(1)) - var testStatusIMPL: TestStatus - testFixture(n, testStatusIMPL, true) + let config = getConfiguration() + if config.testSubject.len == 0: + echo "missing test subject" + quit(QuitFailure) -main() + let path = "tests" / "fixtures" / "GeneralStateTests" + let n = json.parseFile(path / config.testSubject) + var testStatusIMPL: TestStatus + var forks: set[Fork] = {} + forks.incl config.fork + testFixture(n, testStatusIMPL, true, forks) + +when isMainModule: + var message: string + + ## Processing command line arguments + if processArguments(message) != Success: + echo message + quit(QuitFailure) + else: + if len(message) > 0: + echo message + quit(QuitSuccess) + + main() diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index beb4b462a..73c825ccc 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -24,7 +24,7 @@ const FkByzantium: "Byzantium", }.toTable - supportedForks* = [FkFrontier, FkHomestead] + supportedForks* = {FkFrontier, FkHomestead} type Status* {.pure.} = enum OK, Fail, Skip