UIStringParser.purs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. module Test.UIStringParser where
  2. import Prelude
  3. import Data.Date (Month(..))
  4. import Data.Either (Either(..))
  5. import Data.List (List(..), (:))
  6. import Data.List.Types (NonEmptyList(..))
  7. import Data.NonEmpty (NonEmpty(..))
  8. import LinkedIn.UI.Basic.Parser (durationP, medianDotSeparated, monthYearP, timeSpanP)
  9. import LinkedIn.UI.Basic.Types (Duration(..), TimeSpan(..))
  10. import LinkedIn.UI.Strings.Parser (uiStringDurationP, uiStringdotSeparatedP)
  11. import LinkedIn.UI.Strings.Types (UIString(..))
  12. import Parsing (ParseError(..), Position(..), runParser)
  13. import Test.Spec (Spec, describe, it)
  14. import Test.Spec.Assertions (shouldEqual)
  15. import Test.Utils (toMonthYear')
  16. uiStringParserSpec :: Spec Unit
  17. uiStringParserSpec = do
  18. describe "month year parser" do
  19. let run s = runParser s monthYearP
  20. it "works" do
  21. run "fév. 2004" `shouldEqual` Right(toMonthYear' February 2004)
  22. run "juin 2012" `shouldEqual` Right(toMonthYear' June 2012)
  23. describe "timespan parser" do
  24. let run s = runParser s timeSpanP
  25. it "works" do
  26. run "juin 2012 - aujourd’hui" `shouldEqual` Right(TimeSpanToToday (toMonthYear' June 2012))
  27. run "juin 2012 - mai 2021" `shouldEqual` Right(TimeSpanBounded (toMonthYear' June 2012) (toMonthYear' May 2021))
  28. describe "duration parser" do
  29. let run s = runParser s durationP
  30. it "works" do
  31. run "2 ans 3 mois" `shouldEqual` Right(YearsMonth 2 3)
  32. run "1 an 3 mois" `shouldEqual` Right(YearsMonth 1 3)
  33. run "3 mois" `shouldEqual` Right(Months 3)
  34. run "3 ans" `shouldEqual` Right(Years 3)
  35. run "1 an" `shouldEqual` Right(Years 1)
  36. describe "dot separated strings parser" do
  37. let run s = runParser s medianDotSeparated
  38. it "works" do
  39. run "some text 1 · some text 2" `shouldEqual` Right(NonEmptyList(NonEmpty "some text 1" ("some text 2" : Nil)))
  40. run "· some text after a dot" `shouldEqual` Right(NonEmptyList(NonEmpty "some text after a dot" Nil))
  41. run "some text before a dot ·" `shouldEqual` Right(NonEmptyList(NonEmpty "some text before a dot" Nil))
  42. run "string with no dot" `shouldEqual` (Left (ParseError "Expected '•'" (Position { column: 19, index: 18, line: 1 })))
  43. run "· some text in between dots ·" `shouldEqual` Right(NonEmptyList(NonEmpty "some text in between dots" Nil))
  44. describe "UI duration parser" do
  45. let run s = runParser s uiStringDurationP
  46. it "works" do
  47. run "2 ans 3 mois" `shouldEqual` Right(UIStringDuration (YearsMonth 2 3))
  48. describe "UI dot separated string parser" do
  49. let run s = runParser s uiStringdotSeparatedP
  50. it "works" do
  51. run "some text 1 · some text 2" `shouldEqual` Right(UIStringDotSeparated (NonEmptyList(NonEmpty (UIStringPlain "some text 1") ((UIStringPlain "some text 2") : Nil))))