1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 02:44:36 +03:00
semantic/test/Proto3/Roundtrip.hs

116 lines
4.0 KiB
Haskell
Raw Normal View History

2018-06-12 00:10:29 +03:00
{-# LANGUAGE TypeApplications, ScopedTypeVariables #-}
2018-06-05 19:07:47 +03:00
module Proto3.Roundtrip (spec) where
import SpecHelpers
import Data.Blob
2018-06-05 19:07:47 +03:00
import Data.Span
import qualified Data.ByteString.Lazy as L
2018-06-05 19:07:47 +03:00
import Data.Source
import Data.Functor.Foldable
2018-06-05 19:07:47 +03:00
import Proto3.Suite
import qualified Proto3.Wire.Encode as E
2018-06-12 00:10:29 +03:00
import qualified Data.Syntax.Literal as Literal
2018-06-20 18:57:53 +03:00
import qualified Data.Syntax.Statement as Statement
2018-07-05 22:21:25 +03:00
import qualified Data.Syntax.Declaration as Declaration
2018-06-12 00:10:29 +03:00
import Data.Term (Term)
import Data.Diff (Diff)
2018-06-12 00:10:29 +03:00
import Data.Sum
import Language.Ruby.Assignment (Syntax)
2018-06-20 20:41:59 +03:00
import qualified Language.Ruby.Assignment as Ruby
2018-07-23 21:00:10 +03:00
import qualified Language.Python.Syntax as Python
2018-06-12 00:10:29 +03:00
import Data.Functor.Classes
2018-07-13 23:48:29 +03:00
import qualified Data.Syntax.Expression as Expression
2018-06-05 19:07:47 +03:00
shouldRoundtrip :: (Eq a, Show a, Message a) => a -> Expectation
shouldRoundtrip a = go a `shouldBe` Right a
where go = fromByteString . L.toStrict . toLazyByteString
2018-06-05 19:07:47 +03:00
shouldRoundtrip1 :: forall f a. (Show (f a), Eq (f a), Show1 f, Eq1 f, Eq a, Show a, Message1 f, Message a) => f a -> Expectation
shouldRoundtrip1 a = go a `shouldBe` Right a
2018-06-12 00:10:29 +03:00
where go = fromByteString1 . L.toStrict . toLazyByteString1
2018-07-13 23:48:29 +03:00
instance Named1 (Sum '[Literal.Null]) where nameOf1 _ = "NullSyntax"
2018-06-05 19:07:47 +03:00
spec :: Spec
spec = parallel $ do
describe "blobs" $
prop "roundtrips" $
\sp -> shouldRoundtrip @Blob sp
describe "blob pairs" $
prop "roundtrips" $
\sp -> shouldRoundtrip @BlobPair sp
2018-06-05 19:07:47 +03:00
describe "spans" $
prop "roundtrips" $
2018-09-07 02:29:32 +03:00
\x -> shouldRoundtrip @Span x
describe "pos" $
prop "roundtrips" $
\x -> shouldRoundtrip @Pos x
2018-06-05 19:07:47 +03:00
2018-06-12 00:10:29 +03:00
describe "nulls" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.Null @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
describe "text elements" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.TextElement @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
describe "floats" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.Float @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
2018-07-13 23:48:29 +03:00
describe "negate" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Expression.Negate @(Term (Sum '[Literal.Null]) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
describe "booleans" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.Boolean @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
describe "terms of syntax" $
prop "roundtrips" $
\sp -> shouldRoundtrip @(Term (Sum Syntax) ()) (unListableF sp)
-- describe "diffs of syntax" $
-- prop "roundtrips" $
-- \sp -> do
-- shouldRoundtrip @(Diff (Sum Syntax) () ()) (unListableF2 sp)
2018-06-12 00:10:29 +03:00
describe "arrays" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.Array @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
describe "key values" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Literal.KeyValue @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-12 00:10:29 +03:00
2018-06-20 18:57:53 +03:00
describe "statements" $
prop "roundtrips" $
\sp -> shouldRoundtrip @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-20 18:57:53 +03:00
2018-06-22 21:19:01 +03:00
describe "statements1" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Statement.Statements @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-22 21:19:01 +03:00
2018-07-05 22:21:25 +03:00
describe "methods" $
prop "roundtrips" $
\sp -> shouldRoundtrip1 @Declaration.Method @(Term (Sum Syntax) ()) (unListableF sp)
2018-06-05 19:07:47 +03:00
describe "blobs" $ do
it "should roundtrip given a Message instance" $ do
let bl = Blob (fromUTF8 "puts 'hi'") "example.rb" Ruby
shouldRoundtrip bl
describe "languages" $ do
-- If this test broke, it means you've probably added another 'Language'.
-- Add it to the list of languages below and everything should be good,
-- as long as you added it in a way that doesn't break prior Enum encodings.
it "should match up with Enum declarations" $ do
let go :: (Primitive f, MessageField f) => [f] -> [L.ByteString]
go x = E.toLazyByteString . encodePrimitive (fieldNumber 0) <$> x
let ints = [0..fromEnum (maxBound @Language)]
let langs = [Unknown, Go, Haskell, Java, JavaScript, JSON,
JSX, Markdown, Python, Ruby, TypeScript, PHP]
go ints `shouldBe` go langs