Files
eidp-klausuraufgaben/strings/string_manipulation/solution/test_strings.py
2024-01-30 11:15:45 +01:00

49 lines
1.5 KiB
Python

from strings import String
def test_eq_for_str():
test_str = String("this is a test string!")
assert test_str == "this is a test string!", "__eq__ wasn't implemented correctly for `String` and `str`"
def test_strings_contains():
test_str = String("this is a test")
assert test_str.contains("this")
assert not test_str.contains("release")
def test_strings_concat():
test_str = String("")
test_str += "test succesful!"
assert test_str == "test succesful!"
test_str.concat(" Or is it?")
assert test_str == "test succesful! Or is it?"
def test_strings_strip():
test_str = String(" halo? \n")
test_str.strip()
assert test_str == "halo?"
test_str = String(" \n ")
test_str.strip()
assert test_str == ""
def test_strings_replace():
test_str = String("har har har, try replacing thhis")
test_str.replace('har ', 'ha')
assert test_str == "hahahar, try replacing thhis"
test_str.replace('r', '', 1)
assert test_str == "hahaha, try replacing thhis"
test_str.replace('hh', 'h')
assert test_str == "hahaha, try replacing this"
test_str.replace('try replacing this', "replaced")
assert test_str == "hahaha, replaced"
def test_add_pre_suf():
test_str = String(" ")
test_str.add_suffix("suff")
assert test_str == " suff"
test_str.add_prefix("pref")
assert test_str == "pref suff"
def test_join():
assert String(", ").join([1, 2, 3, 4]) == "1, 2, 3, 4"
assert String(", ").join([]) == ""