2 Komitmen 014d6d2252 ... 514d5a17b3

Pembuat SHA1 Pesan Tanggal
  Giedrius Statkevičius 514d5a17b3 test_libtn: add tests for diff() 8 tahun lalu
  Giedrius Statkevičius 8776287cf9 libtn: move duplicated code in diff() to a different fn 8 tahun lalu
2 mengubah file dengan 77 tambahan dan 21 penghapusan
  1. 28 21
      libtn.py
  2. 49 0
      test_libtn.py

+ 28 - 21
libtn.py

@@ -312,6 +312,32 @@ class NotifyApi(object):
 
         return ret
 
+    def inform_user(self, online, data, name):
+        '''
+        Actually inform the user about the change in status.
+
+        Positional arguments:
+        online - is the user `name' online now or not
+        data - information about the user from self.get_status()
+        name - actual name of the user we are talking about
+        '''
+        if online is True:
+            title = repl(data[1], name, self.fmt.notification_title['on'])
+            message = repl(data[1], name, self.fmt.notification_cont['on'])
+            self.log(data[1], name, self.fmt.log_fmt['on'])
+        else:
+            title = repl(data[1], name, self.fmt.notification_title['off'])
+            message = repl(data[1], name, self.fmt.notification_cont['off'])
+            self.log(data[1], name, self.fmt.log_fmt['off'])
+
+        try:
+            show_notification(title, message)
+        except RuntimeError:
+            print('Failed to show a notification:',
+                  file=sys.stderr)
+            print('Title: ' + title, file=sys.stderr)
+            print('Message: ' + message, file=sys.stderr)
+
     def diff(self, new):
         '''
         Check if there is a difference between statuses in `new' and the
@@ -332,28 +358,9 @@ class NotifyApi(object):
                 continue
 
             if ison is True and not self.statuses[name] is True:
-                title = repl(data[1], name, self.fmt.notification_title['on'])
-                message = repl(data[1], name, self.fmt.notification_cont['on'])
-                self.log(data[1], name, self.fmt.log_fmt['on'])
-
-                try:
-                    show_notification(title, message)
-                except RuntimeError:
-                    print('Failed to show a notification:',
-                          file=sys.stderr)
-                    print(name + ' is online')
-
+                self.inform_user(True, data, name)
             elif self.statuses[name] is True and not ison is True:
-                title = repl(data[1], name, self.fmt.notification_title['off'])
-                message = repl(data[1], name, self.fmt.notification_cont['off'])
-                self.log(data[1], name, self.fmt.log_fmt['off'])
-
-                try:
-                    show_notification(title, message)
-                except RuntimeError:
-                    print('Failed to show a notification:',
-                          file=sys.stderr)
-                    print(name + ' is offline')
+                self.inform_user(False, data, name)
 
             self.statuses[name] = ison
 

+ 49 - 0
test_libtn.py

@@ -30,5 +30,54 @@ class LibTest(unittest.TestCase):
         api = libtn.NotifyApi(acc, settings, None, False)
         self.assertEqual(api.check_if_online([acc])[acc][0], False)
 
+    def test_diff(self):
+        api = libtn.NotifyApi('test_account123321', None, None, False)
+        output = {}
+        def assign_output(online, _, name):
+            output[name] = online
+        api.inform_user = assign_output
+
+        # off -> on
+        example = {'abc': (False, {})}
+        api.diff(example)
+        example = {'abc': (True, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], True)
+
+        # on -> off
+        example = {'abc': (False, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], False)
+
+        # off -> error
+        example = {'abc': (None, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], False)
+
+        # error -> on
+        example = {'abc': (True, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], True)
+
+        # on -> error
+        example = {'abc': (None, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], True)
+
+        # error -> off
+        example = {'abc': (False, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], False)
+
+        # off -> error
+        example = {'abc': (None, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], False)
+
+        # error -> error
+        example = {'abc': (None, {})}
+        api.diff(example)
+        self.assertEqual(output['abc'], False)
+
 if __name__ == '__main__':
     unittest.main()