From c017340679fd46186300172dd314fd406a588030 Mon Sep 17 00:00:00 2001 From: Michael Utz Date: Wed, 16 Nov 2022 17:11:56 +0300 Subject: [PATCH] Fix issue with tests that start with "it" --- lua/neotest-pest/utils.lua | 3 +- specs/utils_spec.lua | 515 ++++++++++--------------------------- 2 files changed, 138 insertions(+), 380 deletions(-) diff --git a/lua/neotest-pest/utils.lua b/lua/neotest-pest/utils.lua index 95b86e4..5214131 100644 --- a/lua/neotest-pest/utils.lua +++ b/lua/neotest-pest/utils.lua @@ -57,8 +57,9 @@ end local function make_outputs(test, output_file) logger.info("Pre-output test:", test) local test_attr = test["_attr"] or test[1]["_attr"] + local name = string.gsub(test_attr.name, "it (.*)", "%1") - local test_id = test_attr.file .. separator .. test_attr.name + local test_id = test_attr.file .. separator .. name logger.info("Pest id:", { test_id }) local test_output = { diff --git a/specs/utils_spec.lua b/specs/utils_spec.lua index 6375d19..d1bd929 100644 --- a/specs/utils_spec.lua +++ b/specs/utils_spec.lua @@ -1,407 +1,164 @@ -local utils = require("neotest-phpunit.utils") +local utils = require("neotest-pest.utils") describe("get_test_results", function() - it("parses output when testing a method", function() + it("parses output with whole file", function() local output_file = "/tmp/nvimhYaIPj/3" local xml_output = { testsuites = { testsuite = { _attr = { - assertions = "1", + assertions = "17", errors = "0", failures = "0", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - name = "ExampleTest", - skipped = "0", - tests = "1", - time = "0.002292", - warnings = "0", - }, - testcase = { - _attr = { - assertions = "1", - class = "ExampleTest", - classname = "ExampleTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - line = "7", - name = "test_that_true_is_true", - time = "0.002292", - }, - }, - }, - }, - } - - local expected = { - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php::7"] = { - ["output_file"] = "/tmp/nvimhYaIPj/3", - ["short"] = [[EXAMPLETEST --> PASSED - test_that_true_is_true]], - ["status"] = "passed", - }, - } - - assert.are.same(utils.get_test_results(xml_output, output_file), expected) - end) - - it("parses output when testing a file", function() - local output_file = "/tmp/nvimhYaIPj/3" - local xml_output = { - testsuites = { - testsuite = { - _attr = { - assertions = "2", - errors = "0", - failures = "0", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - name = "TestProject\\UserTest", - skipped = "0", - tests = "2", - time = "0.001525", - warnings = "0", - }, - testcase = { - { - _attr = { - assertions = "3", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "13", - name = "testClassConstructor", - time = "0.000949", - }, - }, - { - _attr = { - assertions = "2", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "22", - name = "testTellName", - time = "0.000135", - }, - }, - }, - }, - }, - } - - local expected = { - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::13"] = { - ["output_file"] = "/tmp/nvimhYaIPj/3", - ["short"] = [[TESTPROJECT.USERTEST --> PASSED - testClassConstructor]], - ["status"] = "passed", - }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::22"] = { - ["output_file"] = "/tmp/nvimhYaIPj/3", - ["short"] = [[TESTPROJECT.USERTEST --> PASSED - testTellName]], - ["status"] = "passed", - }, - } - - assert.are.same(utils.get_test_results(xml_output, output_file), expected) - end) - - it("parses a file even if there is a failure", function() - local output_file = "/tmp/nvimhYaIPj/3" - local xml_output = { - testsuites = { - testsuite = { - _attr = { - assertions = "2", - errors = "0", - failures = "1", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - name = "ExampleTest", - skipped = "0", - tests = "2", - time = "0.001008", - warnings = "0", - }, - testcase = { - { - _attr = { - assertions = "1", - class = "ExampleTest", - classname = "ExampleTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - line = "12", - name = "test_that_false_is_true", - time = "0.000141", - }, - failure = { - [[ExampleTest::test_that_false_is_true -Failed asserting that false is true. - -/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php:14]], - _attr = { - type = "PHPUnit\\Framework\\ExpectationFailedException", - }, - }, - }, - }, - }, - }, - } - - local expected = { - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php::12"] = { - errors = { - { - line = "12", - }, - }, - output_file = "/tmp/nvimhYaIPj/3", - status = "failed", - }, - } - - assert.are.same(utils.get_test_results(xml_output, output_file), expected) - end) - - it("parses multiple files", function() - local output_file = "/tmp/nvimhYaIPj/3" - local xml_output = { - testsuites = { - testsuite = { - _attr = { - assertions = "18", - errors = "0", - failures = "1", name = "", skipped = "0", - tests = "8", - time = "0.001904", - warnings = "0", + tests = "7", + time = "0.006846", + warnings = "0" }, - testsuite = { - { + testsuite = { { + _attr = { + assertions = "1", + errors = "0", + failures = "0", + file = "/Users/michaelutz/Code/neotest-pest/tests/Examples/some/deep/nesting/NestingTest.php", + name = "P\\Tests\\Examples\\some\\deep\\nesting\\NestingTest", + skipped = "0", + tests = "1", + time = "0.002344", + warnings = "0" + }, + testcase = { + _attr = { + assertions = "1", + class = "Tests\\Examples\\some\\deep\\nesting\\NestingTest", + classname = "Tests.Examples.some.deep.nesting.NestingTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Examples/some/deep/nesting/NestingTest.php", + name = "is true", + time = "0.002344" + } + } + }, { + _attr = { + assertions = "15", + errors = "0", + failures = "0", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "P\\Tests\\Feature\\UserTest", + skipped = "0", + tests = "5", + time = "0.004385", + warnings = "0" + }, + testcase = { { + _attr = { + assertions = "3", + class = "Tests\\Feature\\UserTest", + classname = "Tests.Feature.UserTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "class constructor", + time = "0.000941" + } + }, { _attr = { assertions = "2", - errors = "0", - failures = "1", - name = "Unit", - skipped = "0", - tests = "2", - time = "0.001233", - warnings = "0", - }, - testsuite = { - _attr = { - assertions = "2", - errors = "0", - failures = "1", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - name = "ExampleTest", - skipped = "0", - tests = "2", - time = "0.001233", - warnings = "0", - }, - testcase = { - { - _attr = { - assertions = "1", - class = "ExampleTest", - classname = "ExampleTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - line = "7", - name = "test_that_true_is_true", - time = "0.001027", - }, - }, - { - _attr = { - assertions = "1", - class = "ExampleTest", - classname = "ExampleTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php", - line = "13", - name = "this_should_fail", - time = "0.000207", - }, - failure = { - [[ExampleTest::this_should_fail -Failed asserting that false is true. - -/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php:15]], - _attr = { - type = "PHPUnit\\Framework\\ExpectationFailedException", - }, - }, - }, - }, - }, - }, - { + class = "Tests\\Feature\\UserTest", + classname = "Tests.Feature.UserTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "tellName", + time = "0.000494" + } + }, { _attr = { - assertions = "16", - errors = "0", - failures = "0", - name = "Examples", - skipped = "0", - tests = "6", - time = "0.000671", - warnings = "0", - }, - testsuite = { - { - _attr = { - assertions = "15", - errors = "0", - failures = "0", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - name = "TestProject\\UserTest", - skipped = "0", - tests = "5", - time = "0.000646", - warnings = "0", - }, - testcase = { - { - _attr = { - assertions = "3", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "13", - name = "testClassConstructor", - time = "0.000145", - }, - }, - { - _attr = { - assertions = "2", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "22", - name = "testTellName", - time = "0.000143", - }, - }, - { - _attr = { - assertions = "2", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "30", - name = "testTellAge", - time = "0.000026", - }, - }, - { - _attr = { - assertions = "3", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "38", - name = "testAddFavoriteMovie", - time = "0.000149", - }, - }, - { - _attr = { - assertions = "5", - class = "TestProject\\UserTest", - classname = "TestProject.UserTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php", - line = "47", - name = "testRemoveFavoriteMovie", - time = "0.000183", - }, - }, - }, - }, - { - _attr = { - assertions = "1", - errors = "0", - failures = "0", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/some/deep/nesting/NestingTest.php", - name = "NestingTest", - skipped = "0", - tests = "1", - time = "0.000025", - warnings = "0", - }, - testcase = { - _attr = { - assertions = "1", - class = "NestingTest", - classname = "NestingTest", - file = "/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/some/deep/nesting/NestingTest.php", - line = "7", - name = "test_something_that_is_true", - time = "0.000025", - }, - }, - }, - }, + assertions = "2", + class = "Tests\\Feature\\UserTest", + classname = "Tests.Feature.UserTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "can tellAge", + time = "0.000384" + } + }, { + _attr = { + assertions = "3", + class = "Tests\\Feature\\UserTest", + classname = "Tests.Feature.UserTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "addFavoriteMovie", + time = "0.000959" + } + }, { + _attr = { + assertions = "5", + class = "Tests\\Feature\\UserTest", + classname = "Tests.Feature.UserTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php", + name = "removeFavoriteMovie", + time = "0.001608" + } + } } + }, { + _attr = { + assertions = "1", + errors = "0", + failures = "0", + file = "/Users/michaelutz/Code/neotest-pest/tests/Unit/ExampleTest.php", + name = "P\\Tests\\Unit\\ExampleTest", + skipped = "0", + tests = "1", + time = "0.000116", + warnings = "0" }, - }, - }, - }, + testcase = { + _attr = { + assertions = "1", + class = "Tests\\Unit\\ExampleTest", + classname = "Tests.Unit.ExampleTest", + file = "/Users/michaelutz/Code/neotest-pest/tests/Unit/ExampleTest.php", + name = "example", + time = "0.000116" + } + } + } } + } + } } local expected = { - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::13"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[TESTPROJECT.USERTEST --> PASSED - testClassConstructor]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Examples/some/deep/nesting/NestingTest.php::is true"] = { + output_file = output_file, + short = "TESTS.EXAMPLES.SOME.DEEP.NESTING.NESTINGTEST\n-> PASSED - is true", + status = "passed" }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::22"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[TESTPROJECT.USERTEST --> PASSED - testTellName]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php::addFavoriteMovie"] = { + output_file = output_file, + short = "TESTS.FEATURE.USERTEST\n-> PASSED - addFavoriteMovie", + status = "passed" }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::30"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[TESTPROJECT.USERTEST --> PASSED - testTellAge]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php::can tellAge"] = { + output_file = output_file, + short = "TESTS.FEATURE.USERTEST\n-> PASSED - can tellAge", + status = "passed" }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::38"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[TESTPROJECT.USERTEST --> PASSED - testAddFavoriteMovie]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php::class constructor"] = { + output_file = output_file, + short = "TESTS.FEATURE.USERTEST\n-> PASSED - class constructor", + status = "passed" }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/UserTest.php::47"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[TESTPROJECT.USERTEST --> PASSED - testRemoveFavoriteMovie]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php::removeFavoriteMovie"] = { + output_file = output_file, + short = "TESTS.FEATURE.USERTEST\n-> PASSED - removeFavoriteMovie", + status = "passed" }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Examples/some/deep/nesting/NestingTest.php::7"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[NESTINGTEST --> PASSED - test_something_that_is_true]], - status = "passed", - }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php::13"] = { - errors = { - { - line = "13", - }, - }, - output_file = "/tmp/nvimhYaIPj/3", - status = "failed", - }, - ["/Users/Oli/Code/Projects/neotest-phpunit/tests/Unit/ExampleTest.php::7"] = { - output_file = "/tmp/nvimhYaIPj/3", - short = [[EXAMPLETEST --> PASSED - test_that_true_is_true]], - status = "passed", + ["/Users/michaelutz/Code/neotest-pest/tests/Feature/UserTest.php::tellName"] = { + output_file = output_file, + short = "TESTS.FEATURE.USERTEST\n-> PASSED - tellName", + status = "passed" }, + ["/Users/michaelutz/Code/neotest-pest/tests/Unit/ExampleTest.php::example"] = { + output_file = output_file, + short = "TESTS.UNIT.EXAMPLETEST\n-> PASSED - example", + status = "passed" + } } assert.are.same(utils.get_test_results(xml_output, output_file), expected)