|
| 1 | +{- |
| 2 | +Copyright 2023 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +-} |
| 16 | + |
| 17 | +-- | |
| 18 | +-- Description: Tests for the "Rules" module. |
| 19 | +-- Copyright: Copyright 2023 Google LLC |
| 20 | +-- License: Apache-2.0 |
| 21 | + |
| 22 | +module RulesSpec (spec) where |
| 23 | + |
| 24 | +import Data.Aeson |
| 25 | +import Data.Aeson.KeyMap qualified as KeyMap |
| 26 | +import Data.Text qualified as Text |
| 27 | +import Data.Vector qualified as Vector |
| 28 | +import Rules |
| 29 | +import Test.Hspec |
| 30 | + |
| 31 | +spec :: Spec |
| 32 | +spec = do |
| 33 | + describe "add" $ do |
| 34 | + it "adds rules when there was no tool object" $ |
| 35 | + add |
| 36 | + ( Object . KeyMap.singleton "runs" $ |
| 37 | + Array . Vector.fromList $ |
| 38 | + [ Object . KeyMap.singleton "results" . Array . Vector.fromList $ |
| 39 | + [ Object . KeyMap.fromList $ |
| 40 | + [ ("ruleId", String "use 1") |
| 41 | + ], |
| 42 | + Object . KeyMap.fromList $ |
| 43 | + [ ("ruleId", String "use 2"), |
| 44 | + ( "message", |
| 45 | + Object . KeyMap.singleton "text" . String $ |
| 46 | + Text.unlines |
| 47 | + [ "Some warning", |
| 48 | + "Found:", |
| 49 | + " not (x-y < 0 || x-y > 0)", |
| 50 | + "Perhaps:", |
| 51 | + " x == y", |
| 52 | + "Note: this is just better" |
| 53 | + ] |
| 54 | + ) |
| 55 | + ] |
| 56 | + ] |
| 57 | + ] |
| 58 | + ) |
| 59 | + `shouldBe` Object |
| 60 | + ( KeyMap.singleton "runs" $ |
| 61 | + Array . Vector.singleton . Object . KeyMap.fromList $ |
| 62 | + [ ( "tool", |
| 63 | + Object . KeyMap.singleton "driver" $ |
| 64 | + Object . KeyMap.singleton "rules" $ |
| 65 | + Array . Vector.fromList $ |
| 66 | + [ Object . KeyMap.fromList $ |
| 67 | + [ ("id", String "use 1"), |
| 68 | + ("name", String "use 1"), |
| 69 | + ( "shortDescription", |
| 70 | + Object $ KeyMap.singleton "text" $ String "use 1" |
| 71 | + ), |
| 72 | + ( "fullDescription", |
| 73 | + Object $ KeyMap.singleton "text" $ String "use 1" |
| 74 | + ) |
| 75 | + ], |
| 76 | + Object . KeyMap.fromList $ |
| 77 | + [ ("id", String "use 2"), |
| 78 | + ("name", String "use 2"), |
| 79 | + ( "shortDescription", |
| 80 | + Object . KeyMap.singleton "text" $ String "use 2" |
| 81 | + ), |
| 82 | + ( "fullDescription", |
| 83 | + Object . KeyMap.singleton "text" $ String "use 2" |
| 84 | + ) |
| 85 | + ] |
| 86 | + ] |
| 87 | + ), |
| 88 | + ( "results", |
| 89 | + Array . Vector.fromList $ |
| 90 | + [ Object . KeyMap.fromList $ |
| 91 | + [ ("ruleId", String "use 1") |
| 92 | + ], |
| 93 | + Object . KeyMap.fromList $ |
| 94 | + [ ("ruleId", String "use 2"), |
| 95 | + ( "message", |
| 96 | + Object . KeyMap.singleton "text" . String $ |
| 97 | + Text.unlines |
| 98 | + [ "Some warning", |
| 99 | + "Found:", |
| 100 | + " not (x-y < 0 || x-y > 0)", |
| 101 | + "Perhaps:", |
| 102 | + " x == y", |
| 103 | + "Note: this is just better" |
| 104 | + ] |
| 105 | + ) |
| 106 | + ] |
| 107 | + ] |
| 108 | + ) |
| 109 | + ] |
| 110 | + ) |
| 111 | + |
| 112 | + it "adds rules when there was no driver object" $ |
| 113 | + add |
| 114 | + ( Object . KeyMap.singleton "runs" $ |
| 115 | + Array . Vector.fromList $ |
| 116 | + [ Object . KeyMap.fromList $ |
| 117 | + [ ( "tool", |
| 118 | + Object . KeyMap.singleton "extensions" . Array . Vector.singleton $ |
| 119 | + Object . KeyMap.singleton "name" $ |
| 120 | + String "random extension" |
| 121 | + ), |
| 122 | + ( "results", |
| 123 | + Array . Vector.fromList $ |
| 124 | + [ Object . KeyMap.fromList $ |
| 125 | + [ ("ruleId", String "use 1") |
| 126 | + ] |
| 127 | + ] |
| 128 | + ) |
| 129 | + ] |
| 130 | + ] |
| 131 | + ) |
| 132 | + `shouldBe` Object |
| 133 | + ( KeyMap.singleton "runs" $ |
| 134 | + Array . Vector.singleton . Object . KeyMap.fromList $ |
| 135 | + [ ( "tool", |
| 136 | + Object . KeyMap.fromList $ |
| 137 | + [ ( "driver", |
| 138 | + Object . KeyMap.singleton "rules" $ |
| 139 | + Array . Vector.fromList $ |
| 140 | + [ Object . KeyMap.fromList $ |
| 141 | + [ ("id", String "use 1"), |
| 142 | + ("name", String "use 1"), |
| 143 | + ( "shortDescription", |
| 144 | + Object $ KeyMap.singleton "text" $ String "use 1" |
| 145 | + ), |
| 146 | + ( "fullDescription", |
| 147 | + Object $ KeyMap.singleton "text" $ String "use 1" |
| 148 | + ) |
| 149 | + ] |
| 150 | + ] |
| 151 | + ), |
| 152 | + ( "extensions", |
| 153 | + Array . Vector.singleton $ |
| 154 | + Object . KeyMap.singleton "name" $ |
| 155 | + String "random extension" |
| 156 | + ) |
| 157 | + ] |
| 158 | + ), |
| 159 | + ( "results", |
| 160 | + Array . Vector.fromList $ |
| 161 | + [ Object . KeyMap.fromList $ |
| 162 | + [("ruleId", String "use 1")] |
| 163 | + ] |
| 164 | + ) |
| 165 | + ] |
| 166 | + ) |
| 167 | + |
| 168 | + it "does not overwrite existing rules" $ |
| 169 | + add |
| 170 | + ( Object . KeyMap.singleton "runs" $ |
| 171 | + Array . Vector.fromList $ |
| 172 | + [ Object . KeyMap.fromList $ |
| 173 | + [ ( "tool", |
| 174 | + Object . KeyMap.singleton "driver" . Object $ |
| 175 | + KeyMap.singleton "rules" . Array . Vector.fromList $ |
| 176 | + [ Object . KeyMap.fromList $ |
| 177 | + [ ("id", String "use 1"), |
| 178 | + ("name", String "The first rule"), |
| 179 | + ( "shortDescription", |
| 180 | + Object $ KeyMap.singleton "text" $ String "See rule 1" |
| 181 | + ), |
| 182 | + ( "fullDescription", |
| 183 | + Object $ KeyMap.singleton "text" $ String "Really see rule 1" |
| 184 | + ) |
| 185 | + ] |
| 186 | + ] |
| 187 | + ), |
| 188 | + ( "results", |
| 189 | + Array . Vector.fromList $ |
| 190 | + [ Object . KeyMap.fromList $ |
| 191 | + [ ("ruleId", String "use 1") |
| 192 | + ] |
| 193 | + ] |
| 194 | + ) |
| 195 | + ] |
| 196 | + ] |
| 197 | + ) |
| 198 | + `shouldBe` Object |
| 199 | + ( KeyMap.singleton "runs" $ |
| 200 | + Array . Vector.singleton . Object . KeyMap.fromList $ |
| 201 | + [ ( "tool", |
| 202 | + Object . KeyMap.fromList $ |
| 203 | + [ ( "driver", |
| 204 | + Object . KeyMap.singleton "rules" $ |
| 205 | + Array . Vector.fromList $ |
| 206 | + [ Object . KeyMap.fromList $ |
| 207 | + [ ("id", String "use 1"), |
| 208 | + ("name", String "The first rule"), |
| 209 | + ( "shortDescription", |
| 210 | + Object $ KeyMap.singleton "text" $ String "See rule 1" |
| 211 | + ), |
| 212 | + ( "fullDescription", |
| 213 | + Object $ KeyMap.singleton "text" $ String "Really see rule 1" |
| 214 | + ) |
| 215 | + ] |
| 216 | + ] |
| 217 | + ) |
| 218 | + ] |
| 219 | + ), |
| 220 | + ( "results", |
| 221 | + Array . Vector.fromList $ |
| 222 | + [ Object . KeyMap.fromList $ |
| 223 | + [("ruleId", String "use 1")] |
| 224 | + ] |
| 225 | + ) |
| 226 | + ] |
| 227 | + ) |
0 commit comments