Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "trompeloeil_catch.h" |
| 10 | #include "CParser.hpp" |
| 11 | #include "CTree.hpp" |
| 12 | #include "ast.hpp" |
| 13 | |
| 14 | TEST_CASE("cd") |
| 15 | { |
| 16 | CTree tree; |
| 17 | tree.addContainer("", "a"); |
| 18 | tree.addContainer("", "b"); |
| 19 | tree.addContainer("a", "a2"); |
| 20 | tree.addContainer("b", "b2"); |
| 21 | tree.addContainer("a/a2", "a3"); |
| 22 | tree.addContainer("b/b2", "b3"); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 23 | tree.addList("", "list", {"number"}); |
| 24 | tree.addContainer("list", "contInList"); |
| 25 | tree.addList("", "twoKeyList", {"number", "name"}); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 26 | CParser parser(tree); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 27 | std::string input; |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame^] | 28 | std::ostringstream errorStream; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 29 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 30 | SECTION("valid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 31 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 32 | cd_ expected; |
| 33 | |
| 34 | SECTION("container") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 35 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 36 | SECTION("a") |
| 37 | { |
| 38 | input = "cd a"; |
| 39 | expected.m_path.m_nodes.push_back(container_("a")); |
| 40 | } |
| 41 | |
| 42 | SECTION("b") |
| 43 | { |
| 44 | input = "cd b"; |
| 45 | expected.m_path.m_nodes.push_back(container_("b")); |
| 46 | } |
| 47 | |
| 48 | SECTION("a/a2") |
| 49 | { |
| 50 | input = "cd a/a2"; |
| 51 | expected.m_path.m_nodes.push_back(container_("a")); |
| 52 | expected.m_path.m_nodes.push_back(container_("a2")); |
| 53 | } |
| 54 | |
| 55 | SECTION("b/b2") |
| 56 | { |
| 57 | input = "cd b/b2"; |
| 58 | expected.m_path.m_nodes.push_back(container_("b")); |
| 59 | expected.m_path.m_nodes.push_back(container_("b2")); |
| 60 | } |
| 61 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 64 | SECTION("list elements") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 65 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 66 | SECTION("list[number=1]") |
| 67 | { |
| 68 | input = "cd list[number=1]"; |
| 69 | auto keys = std::map<std::string, std::string>{ |
| 70 | {"number", "1"} |
| 71 | }; |
| 72 | expected.m_path.m_nodes.push_back(listElement_("list", keys)); |
| 73 | } |
| 74 | |
| 75 | SECTION("list[number=1]/contInList") |
| 76 | { |
| 77 | input = "cd list[number=1]/contInList"; |
| 78 | auto keys = std::map<std::string, std::string>{ |
| 79 | {"number", "1"}}; |
| 80 | expected.m_path.m_nodes.push_back(listElement_("list", keys)); |
| 81 | expected.m_path.m_nodes.push_back(container_("contInList")); |
| 82 | } |
| 83 | |
| 84 | SECTION("twoKeyList[number=4 name=abcd]") |
| 85 | { |
| 86 | input = "cd twoKeyList[number=4 name=abcd]"; |
| 87 | auto keys = std::map<std::string, std::string>{ |
| 88 | {"number", "4"}, |
| 89 | {"name", "abcd"}}; |
| 90 | expected.m_path.m_nodes.push_back(listElement_("twoKeyList", keys)); |
| 91 | } |
| 92 | |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 93 | } |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame^] | 94 | cd_ command = parser.parseCommand(input, errorStream); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 95 | REQUIRE(command == expected); |
| 96 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 97 | SECTION("invalid input") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 98 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 99 | SECTION("invalid identifiers") |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 100 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 101 | SECTION("nonexistent") |
| 102 | { |
| 103 | input = "cd nonexistent"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 104 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 105 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 106 | SECTION("nonexistent/lol") |
| 107 | { |
| 108 | input = "cd nonexistent/lol"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 109 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 110 | } |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 111 | SECTION("invalid list key identifiers") |
| 112 | { |
| 113 | SECTION("twoKeyList[invalidKey=4]") |
| 114 | { |
| 115 | input = "cd twoKeyList[invalidKey=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | SECTION("twoKeyList[number=4 number=5]") |
| 119 | { |
| 120 | input = "cd twoKeyList[number=4 number=5]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | SECTION("twoKeyList[number=4 name=lol number=7]") |
| 124 | { |
| 125 | input = "cd twoKeyList[number=4 name=lol number=7]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | SECTION("twoKeyList[number=4]") |
| 129 | { |
| 130 | input = "cd twoKeyList[number=4]"; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
Václav Kubernát | 2315c73 | 2018-05-16 20:25:55 +0200 | [diff] [blame^] | 133 | REQUIRE_THROWS(parser.parseCommand(input, errorStream)); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 134 | } |
| 135 | } |