From 6ecb6cfe8e64a3a24073e16713d2b2478b24640f Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 11 Apr 2020 17:09:51 +0530 Subject: [PATCH] tests: add an operator overloading test --- ...erloading_with_string_interpolation_test.v | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 vlib/v/tests/operator_overloading_with_string_interpolation_test.v diff --git a/vlib/v/tests/operator_overloading_with_string_interpolation_test.v b/vlib/v/tests/operator_overloading_with_string_interpolation_test.v new file mode 100644 index 0000000000..c4b7a636ec --- /dev/null +++ b/vlib/v/tests/operator_overloading_with_string_interpolation_test.v @@ -0,0 +1,75 @@ +struct Vec { + x int + y int +} + +pub fn (a Vec) str() string { + return '{$a.x, $a.y}' +} + +fn (a Vec) + (b Vec) Vec { + return Vec { + a.x + b.x, + a.y + b.y + } +} + +fn (a Vec) - (b Vec) Vec { + return Vec { + a.x - b.x, + a.y - b.y + } +} + +fn (a Vec) * (b Vec) Vec { + return Vec { + a.x * b.x, + a.y * b.y + } +} + +fn (a Vec) / (b Vec) Vec { + return Vec { + a.x / b.x, + a.y / b.y + } +} + +fn (a Vec) % (b Vec) Vec { + return Vec { + a.x % b.x, + a.y % b.y + } +} + +fn test_operator_overloading_with_string_interpolation() { + a := Vec{2, 3} + b := Vec{4, 5} + + c := a + b + assert a.x + b.x == c.x + assert a.y + b.y == c.y + + d := a - b + assert a.x - b.x == d.x + assert a.y - b.y == d.y + + e := a * b + assert a.x * b.x == e.x + assert a.y * b.y == e.y + + f := a / b + assert a.x / b.x == f.x + assert a.y / b.y == f.y + + g := a % b + assert a.x % b.x == g.x + assert a.y % b.y == g.y + + assert c.str() == '{6, 8}' + assert d.str() == '{-2, -2}' + assert e.str() == '{8, 15}' + assert f.str() == '{0, 0}' + assert g.str() == '{2, 3}' + +}