Repository: kmyk/online-judge-tools
Pull request #361 (2019-03-08 01:02:07)

#355: fix pre.previous_sibling become None Bug at yukicoder
pre.previous_siblingとtestの修正をまとめて一つにしています。
もし何か間違っていたら再度修正します。


Patch:
diff --git a/onlinejudge/service/yukicoder.py b/onlinejudge/service/yukicoder.py
index e1ad07c..62721de 100644
--- a/onlinejudge/service/yukicoder.py
+++ b/onlinejudge/service/yukicoder.py
@@ -300,7 +300,7 @@ class YukicoderProblem(onlinejudge.type.Problem):
         # parse
         soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding), utils.html_parser)
         samples = onlinejudge._implementation.testcase_zipper.SampleZipper()
-        for pre in soup.find_all('pre'):
+        for pre in soup.select('.sample pre'):
             log.debug('pre: %s', str(pre))
             it = self._parse_sample_tag(pre)
             if it is not None:


Test patch:
diff --git a/tests/command_download_yukicoder.py b/tests/command_download_yukicoder.py
index e4e005b..4c64bca 100644
--- a/tests/command_download_yukicoder.py
+++ b/tests/command_download_yukicoder.py
@@ -8,14 +8,6 @@ class DownloadYukicoderTest(unittest.TestCase):
     def snippet_call_download(self, *args, **kwargs):
         tests.command_download.snippet_call_download(self, *args, **kwargs)
 
-    def test_call_download_yukicoder_no_9002(self):
-        self.snippet_call_download('http://yukicoder.me/problems/no/9002', {
-            'sample-1.in': 'b026324c6904b2a9cb4b88d6d61c81d1',
-            'sample-2.out': 'a403d4dbee7bd783539da3efa43c4399',
-            'sample-2.in': '5b6b41ed9b343fed9cd05a66d36650f0',
-            'sample-1.out': 'b026324c6904b2a9cb4b88d6d61c81d1',
-        })
-
     def test_call_download_yukicoder_100(self):
         self.snippet_call_download('http://yukicoder.me/problems/100', {
             'sample-3.out': '63a98316f78c5127e702db8fbea612a6',
@@ -26,40 +18,6 @@ class DownloadYukicoderTest(unittest.TestCase):
             'sample-1.out': '3bb50ff8eeb7ad116724b56a820139fa',
         })
 
-    def test_call_download_yukicoder_no_104(self):
-        self.snippet_call_download('http://yukicoder.me/problems/no/104', {
-            'sample-3.out': '89c2e8d24975544fd508992593bd4556',
-            'sample-2.in': '82e80aeb6240ed14d8a0f8df1bc4ab19',
-            'sample-2.out': '2737b49252e2a4c0fe4c342e92b13285',
-            'sample-3.in': '802bd42d2ee2af1b0f2165ba526bd1f8',
-            'sample-1.in': 'bc6bccbab69279fd7c28fc71654e57bc',
-            'sample-4.in': '68b329da9893e34099c7d8ad5cb9c940',
-            'sample-1.out': '1dcca23355272056f04fe8bf20edfce0',
-            'sample-4.out': 'b026324c6904b2a9cb4b88d6d61c81d1',
-        })
-
-    def test_call_download_yukicoder_no_400(self):
-        self.snippet_call_download('http://yukicoder.me/problems/no/400', {
-            'sample-3.out': '9c9eb7aca96d568294f752ecd6867cbe',
-            'sample-2.in': '60650122d9941f5b816451ca15d9eff9',
-            'sample-2.out': '90e2a41ae5ea1f30688dcf72ba806b17',
-            'sample-3.in': '9c9eb7aca96d568294f752ecd6867cbe',
-            'sample-1.in': '04b3f6b553cb51fcc486e0a8888c79eb',
-            'sample-4.in': '787786577e5cb219fd38409b5cb7b933',
-            'sample-1.out': '3e1ce07401b37846f4d6aab1efbe771b',
-            'sample-4.out': '60f3f85857568779dbd10bc4fc506f35',
-        })
-
-    def test_call_download_yukicoder_no_260(self):
-        self.snippet_call_download('http://yukicoder.me/problems/no/260/', {
-            'sample-3.out': '15aac79ea3df9c684a6472d156241987',
-            'sample-2.in': 'fdd3527dacad0f949c31f7c4e7fd0c12',
-            'sample-2.out': '49a6957c6e2e1c5ce89cde8898949ae1',
-            'sample-3.in': '0ffaa5e4f1ec8fe18e25698504d659ae',
-            'sample-1.in': 'b16aaad0c06f931e38ad651115b73f56',
-            'sample-1.out': '90e2a51705594d033a3abe9d77b2b7ad',
-        })
-
     @unittest.skipIf('CI' in os.environ, 'login is required')
     def test_call_download_yukicoder_no_8_system(self):
         self.snippet_call_download(
diff --git a/tests/service_yukicoder.py b/tests/service_yukicoder.py
index f2c7cd5..8933dff 100644
--- a/tests/service_yukicoder.py
+++ b/tests/service_yukicoder.py
@@ -1,10 +1,66 @@
 # -*- coding: utf-8 -*-
 import unittest
 
-from onlinejudge.service.yukicoder import YukicoderService
+from onlinejudge.service.yukicoder import YukicoderProblem, YukicoderService
+from onlinejudge.type import *
 
 
-class YukicoderTest(unittest.TestCase):
+class YukicoderProblemTest(unittest.TestCase):
+    def test_from_url(self):
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/9003').problem_no, 9003)
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/100').problem_id, 100)
+        self.assertEqual(YukicoderProblem.from_url('http://yukicoder.me/problems/no/123/').problem_no, 123)
+        self.assertEqual(YukicoderProblem.from_url('http://yukicoder.me/problems/123').problem_id, 123)
+
+    def test_donwload_sample_cases(self):
+        self.assertEqual(YukicoderProblem.from_url('http://yukicoder.me/problems/no/9000').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'yukicoder\n', output_name='サンプル1 出力', output_data=b'Hello World!\n'),
+        ])
+
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/400').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'<<<\n', output_name='サンプル1 出力', output_data=b'>>>\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'<>>\n', output_name='サンプル2 出力', output_data=b'<<>\n'),
+            TestCase(name='sample-3', input_name='サンプル3 入力', input_data=b'>>><<<\n', output_name='サンプル3 出力', output_data=b'>>><<<\n'),
+            TestCase(name='sample-4', input_name='サンプル4 入力', input_data=b'><<><<<><><\n', output_name='サンプル4 出力', output_data=b'><><>>><>><\n'),
+        ])
+
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/260').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'1 100\n', output_name='サンプル1 出力', output_data=b'40\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'114 514\n', output_name='サンプル2 出力', output_data=b'211\n'),
+            TestCase(name='sample-3', input_name='サンプル3 入力', input_data=b'1234 567890\n', output_name='サンプル3 出力', output_data=b'339733\n'),
+        ])
+
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/104').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'LR\n', output_name='サンプル1 出力', output_data=b'5\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'RLL\n', output_name='サンプル2 出力', output_data=b'12\n'),
+            TestCase(name='sample-3', input_name='サンプル3 入力', input_data=b'RLLRLRLRRRLRL\n', output_name='サンプル3 出力', output_data=b'12986\n'),
+            TestCase(name='sample-4', input_name='サンプル4 入力', input_data=b'\n', output_name='サンプル4 出力', output_data=b'1\n'),
+        ])
+
+    def test_donwload_sample_cases_issue_355(self):
+        # see https://github.com/kmyk/online-judge-tools/issues/355
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/649').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'15 3\n1 3\n1 4\n1 5\n2\n2\n1 10\n1 10\n1 1\n2\n1 3\n2\n1 1000\n2\n1 0\n2\n', output_name='サンプル1 出力', output_data=b'5\n-1\n4\n3\n10\n3\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'4 1\n1 10\n1 10\n2\n2\n', output_name='サンプル2 出力', output_data=b'10\n10\n'),
+            TestCase(name='sample-3', input_name='サンプル3 入力', input_data=b'4 2\n1 9\n1 10000000000000000\n1 90000000000000000\n2\n', output_name='サンプル3 出力', output_data=b'10000000000000000\n'),
+            TestCase(name='sample-4', input_name='サンプル4 入力', input_data=b'1 1\n2\n', output_name='サンプル4 出力', output_data=b'-1\n'),
+        ])
+
+    @unittest.expectedFailure
+    def test_donwload_sample_cases_issue_192(self):
+        # see https://github.com/kmyk/online-judge-tools/issues/192
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/750').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'6\n4 5\n3 7\n3 4\n-2 3\n9 1\n3 8\n', output_name='サンプル1 出力', output_data=b'9 1\n4 5\n3 4\n3 7\n3 8\n-2 3\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'3\n3 7\n1 1\n5 3\n', output_name='サンプル2 出力', output_data=b'5 3\n1 1\n3 7\n'),
+            TestCase(name='sample-3', input_name='サンプル3 入力', input_data=b'6\n1 1\n7 4\n0 5\n1 3\n-8 9\n5 1\n', output_name='サンプル3 出力', output_data=b'5 1\n7 4\n1 1\n1 3\n0 5\n-8 9\n'),
+        ])
+        self.assertEqual(YukicoderProblem.from_url('https://yukicoder.me/problems/no/751').download_sample_cases(), [
+            TestCase(name='sample-1', input_name='サンプル1 入力', input_data=b'3\n1 8 3\n2\n6 10\n', output_name='サンプル1 出力', output_data=b'5 72\n'),
+            TestCase(name='sample-2', input_name='サンプル2 入力', input_data=b'2\n-1 1\n3\n-1 1 -1\n', output_name='サンプル2 出力', output_data=b'-1 1\n'),
+        ])
+
+
+class YukicoderOfficialAPITest(unittest.TestCase):
     def test_get_user_10(self):
         data = YukicoderService().get_user(id=10)
         self.assertIn('Id', data)