From 723cf6985d705d6700fab8b4ab92363439d2fe30 Mon Sep 17 00:00:00 2001 From: milkeg Date: Wed, 12 Apr 2017 07:44:58 +0800 Subject: [PATCH] Asserts: Case insensitive "contains" string check (#361) To compare 2 strings, "contains" function is checking in a case sensitive way. Developers might need to have a case insensitive contains to allow them to have more robust test cases. --- docs/content/assertions.fsx | 7 +++++++ src/canopy/canopy.fs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/docs/content/assertions.fsx b/docs/content/assertions.fsx index 149d78d4..f14ef93e 100644 --- a/docs/content/assertions.fsx +++ b/docs/content/assertions.fsx @@ -57,6 +57,13 @@ Assert that one string contains another. *) contains "Log" (read "#logout") +(** +containsInsensitive +-------- +Assert that one string contains (case insensitive) another. +*) +containsInsensitive "Log" (read "#logout") + (** notContains -------- diff --git a/src/canopy/canopy.fs b/src/canopy/canopy.fs index e973de58..129dfcdf 100644 --- a/src/canopy/canopy.fs +++ b/src/canopy/canopy.fs @@ -550,6 +550,13 @@ let contains (value1 : string) (value2 : string) = if (value2.Contains(value1) <> true) then raise (CanopyContainsFailedException(sprintf "contains check failed. %s does not contain %s" value2 value1)) +(* documented/assertions *) +let containsInsensitive (value1 : string) (value2 : string) = + let rules = StringComparison.InvariantCultureIgnoreCase + let contains = value2.IndexOf(value1, rules) + if contains < 0 then + raise (CanopyContainsFailedException(sprintf "contains insensitive check failed. %s does not contain %s" value2 value1)) + (* documented/assertions *) let notContains (value1 : string) (value2 : string) = if (value2.Contains(value1) = true) then