Repository: kmyk/online-judge-tools
Pull request #599 (2019-11-06 10:43:00)

Improve anarchygolf
Related to: #573 #155

In this PR,
- Create `tests/anarchygolf.py` and add some tests.
- 
Apply `utils.parse_content` to `onlinejudge/service/anarchygolf.py`.

- 
Apply `utils.textfile` to `onlinejudge/service/anarchygolf.py` when `Sample Input` is `*NOTHING*`. [Example](http://golf.shinh.org/p.rb?hello+golfers)

- 
Fix `test_call_download_anarchygolf_hello_world` related to above application.


- Fix `_parse_sample_tag` in `onlinejudge/service/anarchygolf.py`.
  - Remove  `utils.textfile`.
  - Add comments.
- Fix `tests/command_download_anarchygolf.py` to adjust above change.





Patch:
diff --git a/onlinejudge/service/anarchygolf.py b/onlinejudge/service/anarchygolf.py
index af6414b..df4b5d7 100644
--- a/onlinejudge/service/anarchygolf.py
+++ b/onlinejudge/service/anarchygolf.py
@@ -60,10 +60,13 @@ class AnarchyGolfProblem(onlinejudge.type.Problem):
             nxt = tag.next_sibling
             while nxt and nxt.string.strip() == '':
                 nxt = nxt.next_sibling
+
+            # This implementation is discussed in https://github.com/kmyk/online-judge-tools/pull/599
             if nxt.name == 'pre':
-                s = utils.textfile(utils.dos2unix(nxt.string.lstrip()))
-            else:
-                s = ''
+                s = utils.dos2unix(nxt.string[1:])
+            elif nxt.name == 'p':
+                s = ''  # *NOTHING* means that the empty string "" is input, not "\n".
+
             return s, name
         return None
 


Test patch:
diff --git a/tests/command_download_anarchygolf.py b/tests/command_download_anarchygolf.py
index 80a9421..aba84c4 100644
--- a/tests/command_download_anarchygolf.py
+++ b/tests/command_download_anarchygolf.py
@@ -9,22 +9,22 @@ class DownloadAnarchyGolfTest(unittest.TestCase):
 
     def test_call_download_anarchygolf_the_b_programming_language(self):
         self.snippet_call_download('http://golf.shinh.org/p.rb?The+B+Programming+Language', {
-            'sample-3.out': 'fcbee46b3b888607abe720d598c75b17',
+            'sample-1.in': '3de90f793f16fad76da1527e09b8e528',
+            'sample-1.out': 'f67b46b3c53308d8a6414b20092a2220',
             'sample-2.in': '810d1189284ef048fc30f80ba7a22c6d',
             'sample-2.out': 'd4e62449830b2a986024d914b194f129',
             'sample-3.in': '7361217616875a437a3d6b41612dacbb',
-            'sample-1.in': '3de90f793f16fad76da1527e09b8e528',
-            'sample-1.out': 'f67b46b3c53308d8a6414b20092a2220',
+            'sample-3.out': 'fcbee46b3b888607abe720d598c75b17',
         })
 
     def test_call_download_anarchygolf_simple_language(self):
         self.snippet_call_download('http://golf.shinh.org/p.rb?simple+language', {
-            'sample-3.out': 'c4211571f7a72cfad092b4dac7b15144',
-            'sample-2.in': '10e10b554ef9bc07d56a514d2f6dab26',
-            'sample-2.out': '48a24b70a0b376535542b996af517398',
-            'sample-3.in': 'f201f3f6606e56f561f8452c9a60210b',
-            'sample-1.in': '9b3c9ece5285bb1bcd1164cec8aa4243',
-            'sample-1.out': '48a24b70a0b376535542b996af517398',
+            'sample-1.in': 'fa50b8b616463173474302ca3e63586b',
+            'sample-1.out': 'a87ff679a2f3e71d9181a67b7542122c',
+            'sample-2.in': '99f5fea6d83f6e55f7d7bca6f7fd1fa3',
+            'sample-2.out': 'a87ff679a2f3e71d9181a67b7542122c',
+            'sample-3.in': '2959791426cb059fce835d1062b655b3',
+            'sample-3.out': '0e04717a220a3bd93e92aceb18d71638',
         })
 
     def test_call_download_anarchygolf_hello_world(self):
diff --git a/tests/service_anarchygolf.py b/tests/service_anarchygolf.py
new file mode 100644
index 0000000..9c30096
--- /dev/null
+++ b/tests/service_anarchygolf.py
@@ -0,0 +1,50 @@
+import unittest
+
+from onlinejudge.service.anarchygolf import AnarchyGolfProblem, AnarchyGolfService
+from onlinejudge.type import TestCase
+
+
+class AnarchyGolfServiceTest(unittest.TestCase):
+    def test_from_url(self):
+        self.assertIsInstance(AnarchyGolfService.from_url('http://golf.shinh.org/'), AnarchyGolfService)
+        self.assertIsInstance(AnarchyGolfService.from_url('http://golf.shinh.org/p.rb?Indent+Space+Alignment'), AnarchyGolfService)
+
+
+class AnarchyGolfProblemTest(unittest.TestCase):
+    def test_download_sample_cases(self):
+        self.assertEqual(AnarchyGolfProblem.from_url('http://golf.shinh.org/p.rb?last+non+zero').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='Sample input', input_data=b'537 527 61 983 69 479 339 179 809 82 766 204 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 179 763 79 267 940 466 53 262 0 0 0 0 0 0 0 0 0 0 723 593 150 520 602 434 802 773 407 380 921 0 0 0 0 0 0 0 0 0 0 294 231 504 438 697 446 417 645 559 991 832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 193 692 62 186 882 336 968 779 941 226 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 93 448 57 617 183 0 0 0 0 0 0 0 0 0 0 957 585 604 817 942 246 454 613 672 21 241 242 0 0 0 0 0 0 0 0 0 0 210 949 377 814 166 729 872 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 403 333 306 775 302 369 479 206 0 0 0 0 0 0 0 0 0 0 0 0 484 70 678 849 996 112 216 635 0 0 0 0 0 0 0 0 0 0 0 971 602 824 881 0 0 0 0 0 0 0 269 931 202 318 0 0 0 0 0 0 0 0 0 0 0 0 0 0', output_name='Sample output', output_data=b'204 262 921 832 226 183 242 872 206 635 881 318'),
+            TestCase(name='sample-2', input_name='Sample input', input_data=b'271 8 943 543 888 589 372 303 619 0 0 0 0 0 0 0 0 0 458 823 375 160 857 44 201 377 140 306 0 0 0 0 0 0 0 0 0 0 812 7 400 475 518 48 0 0 0 0 0 0 0 0 0 0 147 312 182 0 0 0 0 0 0 0 0 595 714 318 277 684 453 353 220 0 0 0 0 0 0 0 194 488 841 453 300 200 462 814 565 0 0 0 0 0 0 0 0 0 0 0 0 0 868 930 265 23 97 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 834 398 49 744 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 643 55 877 457 348 975 977 0 0 0 0 0 0 0 0 0 0 0 0', output_name='Sample output', output_data=b'619 306 48 182 220 565 97 744 977'),
+            TestCase(name='sample-3', input_name='Sample input', input_data=b'45 59 580 260 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 835 611 258 434 588 113 0 0 0 0 0 0 0 0 90 581 493 565 124 256 0 0 0 0 0 0 0 0 0 872 983 456 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 813 314 807 107 406 959 0 0 0 0 0 0 0 0 0 0 0 0 314 545 452 72 488 268 588 61 32 850 0 0 0 0 0 0 0 0 0 0 0 0 626 420 815 179 238 991 193 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 969 972 41 178 386 829 328 883 0 0 0 0 0 0 0 0 0 0 0 357 510 891 109 806 536 134 849 558 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 526 320 733 103 441 880 594 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 123 472 548 520 936 489 707 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 937 104 380 928 434 917 219 869 865 0 0 0 0 0 0 0 0 0 0 303 56 302 125 127 612 260 0 0 0 0 0 0 0 0 0 0 0 0 0', output_name='Sample output', output_data=b'260 113 256 456 959 850 193 883 558 594 707 865 260'),
+        ])
+        self.assertEqual(AnarchyGolfProblem.from_url('http://golf.shinh.org/p.rb?hello+golfers').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='Sample input', input_data=b'', output_name='Sample output', output_data=b'Hello, world!\n'),
+            TestCase(name='sample-2', input_name='Sample input', input_data=b'golfers', output_name='Sample output', output_data=b'Hello, golfers!\n'),
+            TestCase(name='sample-3', input_name='Sample input', input_data=b'shinichiro.h', output_name='Sample output', output_data=b'Hello, shinichiro.h!\n'),
+        ])
+
+    def test_download_sample_cases_issue_155(self):
+        self.assertEqual(AnarchyGolfProblem.from_url('http://golf.shinh.org/p.rb?Digit+Octagon').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='Sample input', input_data=b'', output_name='Sample output', output_data=b'       11111111\
+\n      1222222221\
+\n     123333333321\
+\n    12344444444321\
+\n   1234555555554321\
+\n  123456666666654321\
+\n 12345677777777654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n1234567888888887654321\
+\n 12345677777777654321\
+\n  123456666666654321\
+\n   1234555555554321\
+\n    12344444444321\
+\n     123333333321\
+\n      1222222221\
+\n       11111111'),
+        ])