Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Last active August 29, 2015 14:02
Show Gist options
  • Save kyrathasoft/3946dbdb6de7dc1d20bf to your computer and use it in GitHub Desktop.
Save kyrathasoft/3946dbdb6de7dc1d20bf to your computer and use it in GitHub Desktop.
MyTests.cs has a bunch of boolean methods that test various conditions, returning true or false.
using System;
using System.Text.RegularExpressions;
namespace com.williambryanmiller.testing {
public static class MyTests {
public const string MatchEmailPattern =
@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$";
public static bool appearsToBePureAscii(string p) {
bool test = true;
double nonAsciiChars = 0;
foreach (char myChar in p) {
if (!IsAscii(myChar)) { nonAsciiChars++; }
}
if ((nonAsciiChars / p.Length) > 0.000000001) {
test = false;
}
return test;
}
public static bool containsEllipsis(string p) {
bool test = false;
if (p.Contains("...")) { test = true; }
if (p.Contains(". . .")) { test = true; }
return test;
}
public static bool containsNoSpaces(string p) {
bool test = true;
foreach (char c in p) {
if (c == ' ') {
test = false;
break;
}
}
return test;
}
public static bool containsOnlySpaces(string s) {
bool test = true;
foreach (char c in s) {
if (c != ' ') {
test = false;
break;
}
}
return test;
}
public static bool convertsToInteger(string str) {
//returns true if the string parameter can be converted to an integer
bool test = false;
try {
int x = Convert.ToInt32(str);
test = true;
} catch (FormatException) {
test = false;
}
return test;
}
public static bool endsWithPeriod(string p) {
bool test = false;
char[] myArray = p.ToCharArray();
if (myArray[myArray.GetUpperBound(0)] == '.') { test = true; }
return test;
}
public static bool fallsWithinInclusiveBounds(int i, int lower, int higher) {
//with bounds 0 and 4, returns true if i is 0, 1, 2, 3, or 4
bool test = false;
if (i >= lower) {
if (i <= higher) {
test = true;
}
}
return test;
}
public static bool fallsWithinExclusiveBounds(int i, int lower, int higher) {
//with bounds 0 to 4, returns true if i is 1, 2, or 3
bool test = false;
if (i > lower) {
if (i < higher) {
test = true;
}
}
return test;
}
public static bool isAlphanumeric(char p) {
bool test = true;
int ascii = Convert.ToInt32(p);
if (ascii < 48) { test = false; return test; }
if (ascii >= 58 && ascii < 65) { test = false; return test; }
if (ascii >= 91 && ascii < 97) { test = false; return test; }
if (ascii > 122) { test = false; return test; }
return test;
}
public static bool IsAscii(char c) {
return ((int)c < 128);
}
public static bool isAsciiForUpperCaseLetter(int asciiValue) {
bool test = false;
if (asciiValue >= 65 && asciiValue <= 90) { test = true; }
return test;
}
public static bool isEmail(string sEmail) {
if (sEmail != null) {
return Regex.IsMatch(sEmail, MatchEmailPattern);
} else {
//if sEmail is null, we don't test at all
return false;
}
}
public static bool isNeitherYesNorNo(string p) {
bool test = true;
p = p.Trim().ToLower();
if (p == "yes" || p == "y" || p == "no" || p == "n") {
test = false;
}
return test;
}
public static bool isNonAlphaNumChar(char c) {
if (Char.IsDigit(c)) { return false; }
if (Char.IsLetter(c)) { return false; }
return true;
}
public static bool isNumeric(char p) {
bool test = true;
int ascii = Convert.ToInt32(p);
if (ascii < 48) { test = false; return test; }
if (ascii > 57) { test = false; return test; }
return test;
}
public static bool isVowel(char p) {
bool test = false;
string vowels = "aeiouy";
if (vowels.Contains(p.ToString().ToLower())) { test = true; }
return test;
}
public static bool isUpperCaseLetter(char p) {
bool test = false;
int ascii = Convert.ToInt32(p);
if (ascii >= 65 && ascii <= 90) { test = true; }
return test;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment