UIStringParser.purs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module Test.UIStringParser
  2. ( main
  3. )
  4. where
  5. import Data.Date (Month(..))
  6. import Data.Either (Either(..))
  7. import Effect (Effect)
  8. import LinkedIn.UIElements.Parser (durationP, monthYearP, timeSpanP, uiStringP)
  9. import LinkedIn.UIElements.Types (Duration(..), MonthYear, TimeSpan(..), UIString(..))
  10. import Parsing (runParser, ParseError)
  11. import Prelude (Unit, discard)
  12. import Test.Assert (assertEqual)
  13. import Test.Utils (toMonthYear')
  14. runMonthYear ∷ String → Either ParseError MonthYear
  15. runMonthYear s = runParser s monthYearP
  16. runTimeSpan ∷ String → Either ParseError TimeSpan
  17. runTimeSpan s = runParser s timeSpanP
  18. runDuration ∷ String → Either ParseError Duration
  19. runDuration s = runParser s durationP
  20. runUIString ∷ String → Either ParseError UIString
  21. runUIString s = runParser s uiStringP
  22. testMonthYearParser ∷ Effect Unit
  23. testMonthYearParser = do
  24. assertEqual {
  25. actual: runMonthYear "fév. 2004",
  26. expected: Right(toMonthYear' February 2004)
  27. }
  28. assertEqual {
  29. actual: runMonthYear "juin 2012",
  30. expected: Right(toMonthYear' June 2012)
  31. }
  32. testTimeSpanParser ∷ Effect Unit
  33. testTimeSpanParser = do
  34. assertEqual {
  35. actual: runTimeSpan "juin 2012 - aujourd’hui",
  36. expected: Right(TimeSpanToToday (toMonthYear' June 2012))
  37. }
  38. assertEqual {
  39. actual: runTimeSpan "juin 2012 - mai 2021",
  40. expected: Right(TimeSpanBounded (toMonthYear' June 2012) (toMonthYear' May 2021))
  41. }
  42. testDurationParser ∷ Effect Unit
  43. testDurationParser = do
  44. assertEqual {
  45. actual: runDuration "2 ans 3 mois",
  46. expected: Right(YearsMonth 2 3)
  47. }
  48. assertEqual {
  49. actual: runDuration "1 an 3 mois",
  50. expected: Right(YearsMonth 1 3)
  51. }
  52. assertEqual {
  53. actual: runDuration "3 mois",
  54. expected: Right(Months 3)
  55. }
  56. assertEqual {
  57. actual: runDuration "3 ans",
  58. expected: Right(Years 3)
  59. }
  60. assertEqual {
  61. actual: runDuration "1 an",
  62. expected: Right(Years 1)
  63. }
  64. testUIParserDuration ∷ Effect Unit
  65. testUIParserDuration = do
  66. assertEqual {
  67. actual: runUIString "2 ans 3 mois",
  68. expected: Right(UIStringDuration (YearsMonth 2 3))
  69. }
  70. testUIParserDotSeparated ∷ Effect Unit
  71. testUIParserDotSeparated = do
  72. assertEqual {
  73. actual: runUIString "2 ans 3 mois · some text",
  74. expected: Right(UIStringDotSeparated (UIStringDuration (YearsMonth 2 3)) (UIStringPlain "some text"))
  75. }
  76. assertEqual {
  77. actual: runUIString "· Boulogne-Billancourt, Île-de-France, France",
  78. expected: Right(UIStringDotSeparated (UIStringPlain "") (UIStringPlain "Boulogne-Billancourt, Île-de-France, France"))
  79. }
  80. main :: Effect Unit
  81. main = do
  82. testMonthYearParser
  83. testTimeSpanParser
  84. testDurationParser
  85. testUIParserDuration
  86. testUIParserDotSeparated