-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/white-box-test-generation' into …
…trunk
- Loading branch information
Showing
24 changed files
with
2,821 additions
and
156 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,95 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import System.Environment | ||
import Data.Aeson | ||
import Data.List | ||
import GHC.Generics | ||
import qualified Data.ByteString.Lazy as B | ||
|
||
import Head | ||
import Parser | ||
import Elixir | ||
import Helpers | ||
|
||
getJSON :: FilePath -> IO B.ByteString | ||
getJSON = B.readFile | ||
|
||
data Graph = Graph { nodes :: [Node], edges :: [Edge] } deriving (Show, Generic) | ||
data Node = Node { nodeId :: Integer, label :: String } deriving (Show, Generic) | ||
data Edge = Edge { nodeFrom :: Integer, nodeTo :: Integer } deriving (Show, Generic) | ||
|
||
instance FromJSON Graph where | ||
parseJSON (Object v) = | ||
Graph <$> v .: "objects" | ||
<*> v .: "edges" | ||
|
||
instance FromJSON Node where | ||
parseJSON (Object v) = | ||
Node <$> v .: "_gvid" | ||
<*> v .: "label" | ||
|
||
instance FromJSON Edge where | ||
parseJSON (Object v) = | ||
Edge <$> v .: "tail" | ||
<*> v .: "head" | ||
|
||
|
||
genTests :: String -> Graph -> Either String String | ||
genTests m Graph{nodes=ns, edges=es} = case traverse (testForNode m (Graph ns es)) ns of | ||
Right ts -> Right (header m ++ intercalate "\n" ts ++ "\nend") | ||
Left e -> Left e | ||
|
||
testForNode :: String -> Graph -> Node -> Either String String | ||
testForNode m g Node{nodeId=i, label=l} = do vs <- toMap Node{nodeId=i, label=l} | ||
ss <- statesFromId g i >>= traverse toMap | ||
return (unlines [ | ||
"test \"fromState " ++ show i ++ "\" do", | ||
" variables = " ++ vs, | ||
"", | ||
" expectedStates = [" ++ intercalate ",\n" ss ++ "]", | ||
"", | ||
" actions = " ++ m ++ ".next(variables)", | ||
" states = Enum.map(actions, fn action -> action[:state] end)", | ||
"", | ||
" assert Enum.sort(Enum.uniq(states)) == Enum.sort(Enum.uniq(expectedStates))", | ||
"end" | ||
]) | ||
|
||
statesFromId :: Graph -> Integer -> Either String [Node] | ||
statesFromId Graph{nodes=ns, edges=es} i = let edgesFromId = filter (\Edge{nodeFrom=f, nodeTo=t} -> f == i) es | ||
nodesIdsFromId = map (\Edge{nodeFrom=_, nodeTo=t} -> t) edgesFromId | ||
in traverse (findNode ns) nodesIdsFromId | ||
|
||
findNode :: [Node] -> Integer -> Either String Node | ||
findNode ns n = case find (\Node{nodeId=i, label=_} -> n == i) ns of | ||
Just node -> Right node | ||
Nothing -> Left ("Node with id " ++ show n ++ " could not be found") | ||
|
||
toMap :: Node -> Either String String | ||
toMap Node{nodeId=_, label=l} = case parseState (unescape l) of | ||
Right a -> Right (initialState [] a) | ||
Left e -> Left (show e) | ||
|
||
unescape :: String -> String | ||
unescape [] = [] | ||
unescape [s] = [s] | ||
unescape(c1:c2:cs) = if c1 == '\\' && (c2 == '\\' || c2 == 'n') then (if c2 == 'n' then unescape cs else unescape (c2:cs)) else c1:unescape (c2:cs) | ||
|
||
header :: String -> String | ||
header m = unlines [ | ||
"defmodule " ++ m ++ "Test do", | ||
" use ExUnit.Case", | ||
" doctest " ++ m | ||
] | ||
|
||
main :: IO () | ||
main = do | ||
(moduleName:file:_) <- getArgs | ||
d <- (eitherDecode <$> getJSON file) :: IO (Either String Graph) | ||
case d of | ||
Left err -> putStrLn err | ||
Right ps -> case genTests moduleName ps of | ||
Left err -> putStrLn err | ||
Right s -> let f = "elixir/test/generated_code/" ++ snake moduleName ++ "_test.exs" | ||
in writeFile f s |
This file contains 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,33 @@ | ||
import System.Environment | ||
import Data.List | ||
import Parser | ||
import Elixir | ||
import Helpers | ||
|
||
testFile moduleName testTrace testName = unlines [ | ||
"defmodule Mix.Tasks." ++ testName ++ " do", | ||
" @moduledoc \"Runs blackblox testing using the oracle\"", | ||
" @shortdoc \"Runs trace checking for a witness\"", | ||
" use Mix.Task", | ||
"", | ||
" @impl Mix.Task", | ||
" def run(_) do", | ||
" trace = [", | ||
intercalate ",\n" testTrace, | ||
" ]", | ||
"", | ||
" oracle = spawn(TraceCheckerOracle, :start, [trace])", | ||
" " ++ moduleName ++ ".main(oracle, Enum.at(trace, 0), 0)", | ||
" end", | ||
"end" | ||
] | ||
|
||
main :: IO () | ||
main = do | ||
(file:moduleName:testName:_) <- getArgs | ||
f <- readFile file | ||
case parseTrace f of | ||
Right ss -> let content = testFile moduleName (map (initialState []) ss) testName | ||
outFile = "elixir/lib/mix/tasks/" ++ snake testName ++ ".ex" | ||
in writeFile outFile content | ||
Left e -> print e |
This file contains 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,2 @@ | ||
#!/bin/bash | ||
dot -Txdot_json $1 | jq '.objects = (.objects | map(. | select(.label!=null)))' > $2 |
Oops, something went wrong.