UIStringParser.purs 2.3 KB

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