File: StringParsing.hs

package info (click to toggle)
haskell-options 1.2.1.1-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 184 kB
  • sloc: haskell: 2,143; ansic: 91; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,981 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}

-- Copyright (C) 2012 John Millikin <jmillikin@gmail.com>
--
-- See license.txt for details
module OptionsTests.StringParsing
	( suite_StringParsing
	) where

import           Control.Applicative
import           Test.Chell

import           Options

data StringOptions = StringOptions
	{ optString :: String
	, optString_defA :: String
	, optString_defU :: String
	}

instance Options StringOptions where
	defineOptions = pure StringOptions
		<*> simpleOption "string" "" ""
		-- String, ASCII default
		<*> simpleOption "string_defA" "a" ""
		-- String, Unicode default
		<*> simpleOption "string_defU" "\12354" ""

suite_StringParsing :: Suite
suite_StringParsing = suite "string-parsing"
	[ test_Defaults
	, test_Ascii
	, test_UnicodeValid
	, test_UnicodeInvalid
	]

test_Defaults :: Test
test_Defaults = assertions "defaults" $ do
	let opts = defaultOptions
	
	$expect (equal (optString_defA opts) "a")
	$expect (equal (optString_defU opts) "\12354")

test_Ascii :: Test
test_Ascii = assertions "ascii" $ do
	let parsed = parseOptions ["--string=a"]
	let Just opts = parsedOptions parsed
	
	$expect (equal (optString opts) "a")

test_UnicodeValid :: Test
test_UnicodeValid = assertions "unicode-valid" $ do
#if defined(OPTIONS_ENCODING_UTF8)
	let parsed = parseOptions ["--string=\227\129\130"]
#else
	let parsed = parseOptions ["--string=\12354"]
#endif
	let Just opts = parsedOptions parsed
	
	$expect (equal (optString opts) "\12354")

test_UnicodeInvalid :: Test
test_UnicodeInvalid = assertions "unicode-invalid" $ do
#if __GLASGOW_HASKELL__ >= 704
	let parsed = parseOptions ["--string=\56507"]
	let expectedString = "\56507"
#elif __GLASGOW_HASKELL__ >= 702
	let parsed = parseOptions ["--string=\61371"]
	let expectedString = "\61371"
#else
	let parsed = parseOptions ["--string=\187"]
	let expectedString = "\56507"
#endif
	let Just opts = parsedOptions parsed
	
	$expect (equal (optString opts) expectedString)