From 248beffa2f29741093feee220c6d745cb3bfab3a Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Thu, 8 Dec 2022 01:16:00 -0800 Subject: [PATCH] Add tests --- mpris/mpris_test.go | 84 +++++++++++++++++++++++++++++++++++++++ translit/chinese.go | 2 - translit/translit_test.go | 47 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 mpris/mpris_test.go create mode 100644 translit/translit_test.go diff --git a/mpris/mpris_test.go b/mpris/mpris_test.go new file mode 100644 index 0000000..2e989c8 --- /dev/null +++ b/mpris/mpris_test.go @@ -0,0 +1,84 @@ +package mpris + +import ( + "reflect" + "testing" + + "github.com/godbus/dbus/v5" +) + +// TestParsePropertiesChanged checks the parsePropertiesChanged function to +// make sure it correctly parses a DBus PropertiesChanged signal. +func TestParsePropertiesChanged(t *testing.T) { + // Create a DBus message + msg := &dbus.Message{ + Body: []interface{}{ + "com.example.Interface", + map[string]dbus.Variant{ + "Property1": dbus.MakeVariant(true), + "Property2": dbus.MakeVariant("Hello, world!"), + }, + []string{}, + }, + } + + // Parse the message + iface, changed, ok := parsePropertiesChanged(msg) + if !ok { + t.Error("Expected parsePropertiesChanged to return true, but got false") + } + + // Check the parsed values + expectedIface := "com.example.Interface" + if iface != expectedIface { + t.Errorf("Expected iface to be %q, but got %q", expectedIface, iface) + } + + expectedChanged := map[string]dbus.Variant{ + "Property1": dbus.MakeVariant(true), + "Property2": dbus.MakeVariant("Hello, world!"), + } + if !reflect.DeepEqual(changed, expectedChanged) { + t.Errorf("Expected changed to be %v, but got %v", expectedChanged, changed) + } + + // Test a message with an invalid number of arguments + msg = &dbus.Message{ + Body: []interface{}{ + "com.example.Interface", + }, + } + _, _, ok = parsePropertiesChanged(msg) + if ok { + t.Error("Expected parsePropertiesChanged to return false, but got true") + } + + // Test a message with an invalid first argument + msg = &dbus.Message{ + Body: []interface{}{ + 123, + map[string]dbus.Variant{ + "Property1": dbus.MakeVariant(true), + "Property2": dbus.MakeVariant("Hello, world!"), + }, + []string{}, + }, + } + _, _, ok = parsePropertiesChanged(msg) + if ok { + t.Error("Expected parsePropertiesChanged to return false, but got true") + } + + // Test a message with an invalid second argument + msg = &dbus.Message{ + Body: []interface{}{ + "com.example.Interface", + 123, + []string{}, + }, + } + _, _, ok = parsePropertiesChanged(msg) + if ok { + t.Error("Expected parsePropertiesChanged to return false, but got true") + } +} diff --git a/translit/chinese.go b/translit/chinese.go index 97f3030..a8b1099 100644 --- a/translit/chinese.go +++ b/translit/chinese.go @@ -35,8 +35,6 @@ func (ct *ChineseTranslit) Transliterate(s string) string { // Reset temporary buffer tmpBuf.Reset() } - // Write character to output - outBuf.WriteRune(char) } } // If buffer contains characters diff --git a/translit/translit_test.go b/translit/translit_test.go new file mode 100644 index 0000000..07cc678 --- /dev/null +++ b/translit/translit_test.go @@ -0,0 +1,47 @@ +package translit + +import "testing" + +func TestTransliterate(t *testing.T) { + type testCase struct { + name string + input string + expected string + } + + var cases = []testCase{ + {"eASCII", "œª°«»", `oeao""`}, + {"Scandinavian", "ÆæØøÅå", "AeaeOeoeAaaa"}, + {"German", "äöüÄÖÜßẞ", "aeoeueAeOeUessSS"}, + {"Hebrew", "אבגדהוזחטיכלמנסעפצקרשתףץךםן", "abgdhuzkhtyclmns'ptskrshthftschmn"}, + {"Greek", "αάβγδεέζηήθιίϊΐκλμνξοόπρσςτυύϋΰφχψωώΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ", "aavgdeeziithiiiiklmnksooprsstyyyyfchpsooAABGDEEZIIThIIIKLMNKsOOPRSTYYYFChPsOO"}, + {"Russian", "Ёё", "Йoйo"}, + {"Ukranian", "ґєіїҐЄІЇ", "ghjeijiGhJeIJI"}, + {"Arabic", "ابتثجحخدذرزسشصضطظعغفقكلمنهويىﺓآئإؤأء٠١٢٣٤٥٦٧٨٩", "abtthj75dthrzssh99'66'33'fqklmnhwya2222220123456789"}, + {"Farsi", "پچژکگی\u200c؟٪؛،۱۲۳۴۵۶۷۸۹۰»«َُِّ", "pchzhkgy ?%;:1234567890<>eao"}, + {"Polish", "Łł", "Ll"}, + {"Lithuanian", "ąčęėįšųūž", "aceeisuuz"}, + {"Estonian", "äÄöõÖÕüÜ", "aAooOOuU"}, + {"Icelandic", "ÞþÐð", "ThthDd"}, + {"Czech", "řěýáíéóúůďťň", "reyaieouudtn"}, + {"French", "àâéèêëùüÿç", "aaeeeeuuyc"}, + {"Romanian", "ăĂâÂîÎșȘțȚşŞţŢ„”", `aAaAiIsStTsStT""`}, + { + "Emoji", + "😂🤣😊☺️😌😃😁😋😛😜🙃😎😶😩😕😏💜💖💗❤️💕💞💘💓💚💙💟❣️💔😱😮😯😝🤔😔😍😘😚😙👍👌🤞✌️🌄🌞🤗🌻🥱🙄🔫🥔😬✨🌌💀😅😢💯🔥😉😴💤", + `XDXD:):):):D:D:P:P;P(:8):#-_-:(:‑J<3<3<3<3<3<3<3<3<3<3<3<3!