diff mbox series

[04/10] test: Add some setexpr regex tests

Message ID 20201101211544.3579850-5-sjg@chromium.org
State Accepted
Commit e713124e352efe3141af6a4ffc83f6dba449a177
Delegated to: Tom Rini
Headers show
Series setexpr: Correct various bugs and add tests plus string support | expand

Commit Message

Simon Glass Nov. 1, 2020, 9:15 p.m. UTC
Add tests for the setexpr regex commands.

Note that these tests currently crash on sandbox due to an existing bug in
the setexpr implementation, so two of the tests are commented out.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/cmd/setexpr.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

Comments

Tom Rini Dec. 2, 2020, 9:22 p.m. UTC | #1
On Sun, Nov 01, 2020 at 02:15:38PM -0700, Simon Glass wrote:

> Add tests for the setexpr regex commands.
> 
> Note that these tests currently crash on sandbox due to an existing bug in
> the setexpr implementation, so two of the tests are commented out.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/next, thanks!
diff mbox series

Patch

diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
index e950c380ce0..de54561917c 100644
--- a/test/cmd/setexpr.c
+++ b/test/cmd/setexpr.c
@@ -151,6 +151,64 @@  static int setexpr_test_oper(struct unit_test_state *uts)
 }
 SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
 
+/* Test 'setexpr' command with regex */
+static int setexpr_test_regex(struct unit_test_state *uts)
+{
+	char *buf, *val;
+
+	buf = map_sysmem(0, BUF_SIZE);
+
+	/* Single substitution */
+	ut_assertok(run_command("setenv fred 'this is a test'", 0));
+	ut_assertok(run_command("setexpr fred sub is us", 0));
+	val = env_get("fred");
+	ut_asserteq_str("thus is a test", val);
+
+	/* Global substitution */
+	ut_assertok(run_command("setenv fred 'this is a test'", 0));
+	if (0) {
+		/* Causes a crash at present due to a bug in setexpr */
+		ut_assertok(run_command("setexpr fred gsub is us", 0));
+		val = env_get("fred");
+		ut_asserteq_str("thus us a test", val);
+	}
+	/* Global substitution */
+	ut_assertok(run_command("setenv fred 'this is a test'", 0));
+	ut_assertok(run_command("setenv mary 'this is a test'", 0));
+	ut_assertok(run_command("setexpr fred gsub is us \"${mary}\"", 0));
+	val = env_get("fred");
+	ut_asserteq_str("thus us a test", val);
+	val = env_get("mary");
+	ut_asserteq_str("this is a test", val);
+
+	unmap_sysmem(buf);
+
+	return 0;
+}
+SETEXPR_TEST(setexpr_test_regex, UT_TESTF_CONSOLE_REC);
+
+/* Test 'setexpr' command with regex replacement that expands the string */
+static int setexpr_test_regex_inc(struct unit_test_state *uts)
+{
+	char *buf, *val;
+
+	buf = map_sysmem(0, BUF_SIZE);
+
+	ut_assertok(run_command("setenv fred 'this is a test'", 0));
+	if (0) {
+		/* Causes a crash at present due to a bug in setexpr */
+		ut_assertok(run_command("setexpr fred gsub is much_longer_string",
+					0));
+		val = env_get("fred");
+		ut_asserteq_str("thmuch_longer_string much_longer_string a test",
+				val);
+	}
+	unmap_sysmem(buf);
+
+	return 0;
+}
+SETEXPR_TEST(setexpr_test_regex_inc, UT_TESTF_CONSOLE_REC);
+
 int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	struct unit_test *tests = ll_entry_start(struct unit_test,