|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import unittest |
| 4 | +from argparse import ArgumentError |
| 5 | +from types import ModuleType |
| 6 | + |
| 7 | +from pythainlp import __main__, cli |
| 8 | + |
| 9 | + |
| 10 | +class TestMainPackage(unittest.TestCase): |
| 11 | + def test_cli_main(self): |
| 12 | + # call with no argument, should exit with 2 |
| 13 | + with self.assertRaises(SystemExit) as ex: |
| 14 | + __main__.main() |
| 15 | + self.assertEqual(ex.exception.code, 2) |
| 16 | + |
| 17 | + with self.assertRaises((ArgumentError, SystemExit)): |
| 18 | + self.assertIsNone(__main__.main(["thainlp"])) |
| 19 | + |
| 20 | + with self.assertRaises((ArgumentError, SystemExit)): |
| 21 | + self.assertIsNone( |
| 22 | + __main__.main(["thainlp", "NOT_EXIST", "command"]) |
| 23 | + ) |
| 24 | + |
| 25 | + self.assertIsNone(__main__.main(["thainlp", "data", "path"])) |
| 26 | + |
| 27 | + def test_cli_data(self): |
| 28 | + self.assertIsInstance(getattr(cli, "data"), ModuleType) |
| 29 | + |
| 30 | + with self.assertRaises(SystemExit) as ex: |
| 31 | + cli.data.App(["thainlp", "data"]) |
| 32 | + self.assertEqual(ex.exception.code, 2) |
| 33 | + |
| 34 | + self.assertIsNotNone(cli.data.App(["thainlp", "data", "catalog"])) |
| 35 | + self.assertIsNotNone(cli.data.App(["thainlp", "data", "path"])) |
| 36 | + self.assertIsNotNone(cli.data.App(["thainlp", "data", "get", "test"])) |
| 37 | + self.assertIsNotNone(cli.data.App(["thainlp", "data", "info", "test"])) |
| 38 | + self.assertIsNotNone(cli.data.App(["thainlp", "data", "rm", "test"])) |
| 39 | + self.assertIsNotNone( |
| 40 | + cli.data.App(["thainlp", "data", "get", "NOT_EXIST"]) |
| 41 | + ) |
| 42 | + self.assertIsNotNone( |
| 43 | + cli.data.App(["thainlp", "data", "info", "NOT_EXIST"]) |
| 44 | + ) |
| 45 | + self.assertIsNotNone( |
| 46 | + cli.data.App(["thainlp", "data", "rm", "NOT_EXIST"]) |
| 47 | + ) |
| 48 | + |
| 49 | + def test_cli_soundex(self): |
| 50 | + self.assertIsInstance(getattr(cli, "soundex"), ModuleType) |
| 51 | + |
| 52 | + with self.assertRaises(SystemExit) as ex: |
| 53 | + cli.data.App(["thainlp", "soundex"]) |
| 54 | + self.assertEqual(ex.exception.code, 2) |
| 55 | + |
| 56 | + self.assertIsNotNone(cli.soundex.App(["thainlp", "soundex", "ทดสอบ"])) |
| 57 | + |
| 58 | + def test_cli_tag(self): |
| 59 | + self.assertIsInstance(getattr(cli, "tag"), ModuleType) |
| 60 | + |
| 61 | + with self.assertRaises(SystemExit) as ex: |
| 62 | + cli.data.App(["thainlp", "tag"]) |
| 63 | + self.assertEqual(ex.exception.code, 2) |
| 64 | + |
| 65 | + self.assertIsNotNone( |
| 66 | + cli.tag.App( |
| 67 | + [ |
| 68 | + "thainlp", |
| 69 | + "tag", |
| 70 | + "pos", |
| 71 | + "-s", |
| 72 | + " ", |
| 73 | + "มอเตอร์ไซค์ ความว่างเปล่า", |
| 74 | + ] |
| 75 | + ) |
| 76 | + ) |
| 77 | + self.assertIsNotNone( |
| 78 | + cli.tag.App( |
| 79 | + [ |
| 80 | + "thainlp", |
| 81 | + "tag", |
| 82 | + "role", |
| 83 | + "-s", |
| 84 | + " ", |
| 85 | + "มอเตอร์ไซค์ ความว่างเปล่า", |
| 86 | + ] |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + def test_cli_tokenize(self): |
| 91 | + self.assertIsInstance(getattr(cli, "tokenize"), ModuleType) |
| 92 | + |
| 93 | + with self.assertRaises(SystemExit) as ex: |
| 94 | + cli.data.App(["thainlp", "tokenize"]) |
| 95 | + self.assertEqual(ex.exception.code, 2) |
| 96 | + |
| 97 | + self.assertIsNotNone( |
| 98 | + cli.tokenize.App( |
| 99 | + ["thainlp", "tokenize", "NOT_EXIST", "ไม่มีอยู่ จริง"] |
| 100 | + ) |
| 101 | + ) |
| 102 | + self.assertIsNotNone( |
| 103 | + cli.tokenize.App( |
| 104 | + [ |
| 105 | + "thainlp", |
| 106 | + "tokenize", |
| 107 | + "subword", |
| 108 | + "-s", |
| 109 | + "|", |
| 110 | + "ถ้าฉันยิงกระต่ายได้ ฉันก็ยิงฟาสซิสต์ได้", |
| 111 | + ] |
| 112 | + ) |
| 113 | + ) |
| 114 | + self.assertIsNotNone( |
| 115 | + cli.tokenize.App( |
| 116 | + [ |
| 117 | + "thainlp", |
| 118 | + "tokenize", |
| 119 | + "syllable", |
| 120 | + "-s", |
| 121 | + "|", |
| 122 | + "-w", |
| 123 | + "ถ้าฉันยิงกระต่ายได้ ฉันก็ยิงฟาสซิสต์ได้", |
| 124 | + ] |
| 125 | + ) |
| 126 | + ) |
| 127 | + self.assertIsNotNone( |
| 128 | + cli.tokenize.App( |
| 129 | + [ |
| 130 | + "thainlp", |
| 131 | + "tokenize", |
| 132 | + "word", |
| 133 | + "-nw", |
| 134 | + "-a", |
| 135 | + "newmm", |
| 136 | + "-s", |
| 137 | + "|", |
| 138 | + "ถ้าฉันยิงกระต่ายได้ ฉันก็ยิงฟาสซิสต์ได้", |
| 139 | + ] |
| 140 | + ) |
| 141 | + ) |
| 142 | + self.assertIsNotNone( |
| 143 | + cli.tokenize.App( |
| 144 | + [ |
| 145 | + "thainlp", |
| 146 | + "tokenize", |
| 147 | + "sent", |
| 148 | + "-s", |
| 149 | + "|", |
| 150 | + ( |
| 151 | + "ถ้าฉันยิงกระต่ายได้ ฉันก็ยิงฟาสซิสต์ได้" |
| 152 | + "กระสุนสำหรับสมองของคุณวันนี้" |
| 153 | + "แต่คุณก็จะลืมมันไปทั้งหมดอีกครั้ง" |
| 154 | + ), |
| 155 | + ] |
| 156 | + ) |
| 157 | + ) |
0 commit comments