From 68104849904221b6d8fce38aa580945fe39cdd8b Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Wed, 13 Sep 2023 11:48:57 +0200 Subject: [PATCH] Fix `in()` Fix conditional with unexpected values. (hbz/lobid-resources#1889) --- .../metafacture/metafix/FixConditional.java | 2 + .../metafacture/metafix/MetafixIfTest.java | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixConditional.java b/metafix/src/main/java/org/metafacture/metafix/FixConditional.java index 9b09dafb..b41165b8 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixConditional.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixConditional.java @@ -89,9 +89,11 @@ public boolean test(final Metafix metafix, final Record record, final ListextractType((m, c) -> m .ifArray(a1 -> value2.matchType() .ifArray(a2 -> c.accept(a1.equals(a2))) + .orElse(v -> c.accept(false)) ) .ifHash(h1 -> value2.matchType() .ifHash(h2 -> c.accept(h1.equals(h2))) + .orElse(v -> c.accept(false)) ) .ifString(s1 -> value2.matchType() .ifArray(a2 -> c.accept(a2.stream().anyMatch(value1::equals))) diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java index 849aa431..5b8000dc 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java @@ -1430,6 +1430,89 @@ public void shouldContainHashInHash() { ); } + @Test + public void shouldNotContainArrayInString() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "unless in(bar,foo)", + " add_field(test,ok)", + "end" + ), + i -> { + i.startRecord("1"); + i.literal("foo", "1"); + i.literal("bar", "1"); + i.literal("bar", "2"); + i.literal("bar", "3"); + i.endRecord(); + i.startRecord("2"); + i.literal("foo", "42"); + i.literal("bar", "1"); + i.literal("bar", "2"); + i.literal("bar", "3"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().literal("foo", "1"); + o.get().literal("bar", "1"); + o.get().literal("bar", "2"); + o.get().literal("bar", "3"); + o.get().literal("test", "ok"); + o.get().endRecord(); + o.get().startRecord("2"); + o.get().literal("foo", "42"); + o.get().literal("bar", "1"); + o.get().literal("bar", "2"); + o.get().literal("bar", "3"); + o.get().literal("test", "ok"); + o.get().endRecord(); + } + ); + } + + @Test + public void shouldNotContainHashInString() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "unless in(bar,foo)", + " add_field(test,ok)", + "end" + ), + i -> { + i.startRecord("1"); + i.literal("foo", "name"); + i.startEntity("bar"); + i.literal("name", "Patrick"); + i.endEntity(); + i.endRecord(); + i.startRecord("2"); + i.literal("foo", "name"); + i.startEntity("bar"); + i.startEntity("deep"); + i.literal("name", "Nicolas"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().literal("foo", "name"); + o.get().startEntity("bar"); + o.get().literal("name", "Patrick"); + o.get().endEntity(); + o.get().literal("test", "ok"); + o.get().endRecord(); + o.get().startRecord("2"); + o.get().literal("foo", "name"); + o.get().startEntity("bar"); + o.get().startEntity("deep"); + o.get().literal("name", "Nicolas"); + f.apply(2).endEntity(); + o.get().literal("test", "ok"); + o.get().endRecord(); + } + ); + } + @Test public void shouldReportArrayAsArray() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(