Skip to content

Commit

Permalink
add unit tests for url encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Sep 17, 2024
1 parent 91d8a64 commit 58b41a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ bazel_dep(
version = "6.0.2",
dev_dependency = True,
)
bazel_dep(
name = "rules_testing",
version = "0.6.0",
dev_dependency=True,
)
bazel_dep(
name = "stardoc",
version = "0.7.0",
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/url_encoding/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load(":test_url_encoding.bzl", "url_encoding_test_suite")

url_encoding_test_suite(name="test_url_encoding")
29 changes: 29 additions & 0 deletions tests/unit/url_encoding/test_url_encoding.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@rules_testing//lib:analysis_test.bzl", "test_suite", "analysis_test")
load("@rules_testing//lib:util.bzl", "util")
load("//gcs/private:url_encoding.bzl", "url_encode", "url_decode")

def _test_url_encode(env):
env.expect.that_str(url_encode("foo")).equals("foo")
env.expect.that_str(url_encode("a/b/c")).equals("a%2fb%2fc")
env.expect.that_str(url_encode("🖥️")).equals("%f0%9f%96%a5%ef%b8%8f")

def _test_url_decode(env):
env.expect.that_str(url_decode("foo")).equals("foo")
env.expect.that_str(url_decode("a%2fb%2fc")).equals("a/b/c")
env.expect.that_str(url_decode("%f0%9f%96%a5%ef%b8%8f")).equals("🖥️")

def _test_invariant_holds(env):
_invariant(env, """ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~""")

def _invariant(env, s):
env.expect.that_str(url_decode(url_encode(s))).equals(s)

def url_encoding_test_suite(name):
test_suite(
name = name,
basic_tests = [
_test_url_encode,
_test_url_decode,
_test_invariant_holds,
]
)

0 comments on commit 58b41a5

Please sign in to comment.