Created
September 18, 2023 05:30
-
-
Save aidrecabrera/eb358543fbc5c50651635ae847ae0046 to your computer and use it in GitHub Desktop.
(Revision?) - GUI Elements Tester: C# Lesson 1 / Object-Oriented Programming Concepts, specifically in the TOPIC 9 Polymorphism.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Tester | |
{ | |
private static List<FieldInfo> GetAllFields(List<FieldInfo> fields, Type type) | |
{ | |
fields.AddRange(type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)); | |
if (type.BaseType != null) | |
{ | |
GetAllFields(fields, type.BaseType); | |
} | |
return fields; | |
} | |
private static FieldInfo GetField(object obj, string fieldName) | |
{ | |
List<FieldInfo> fields = GetAllFields(new List<FieldInfo>(), obj.GetType()); | |
foreach (FieldInfo f in fields) | |
{ | |
if (f.Name.Equals(fieldName)) | |
{ | |
return f; | |
} | |
} | |
return null; | |
} | |
private static MethodInfo GetMethod(object obj, string methodName) | |
{ | |
MethodInfo[] methods = obj.GetType().GetMethods(); | |
foreach (MethodInfo m in methods) | |
{ | |
if (m.Name.Equals(methodName)) | |
{ | |
return m; | |
} | |
} | |
return null; | |
} | |
public static void Test(InputElement inputElement) | |
{ | |
Console.Write("Enter code: "); | |
int code = Convert.ToInt32(Console.ReadLine()); | |
if (code == 1) | |
{ | |
FieldInfo maxLength = Tester.GetField(inputElement, "maxLength"); | |
FieldInfo value = Tester.GetField(inputElement, "value"); | |
if((maxLength != null && maxLength.IsPrivate) && | |
(value != null && value.IsPrivate) | |
) | |
{ | |
if (inputElement is PasswordInputElement) | |
{ | |
FieldInfo allowedCharacters = Tester.GetField(inputElement, "allowedCharacters"); | |
if(!(allowedCharacters != null && allowedCharacters.IsPrivate)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 2) | |
{ | |
MethodInfo GetContent = Tester.GetMethod(inputElement, "GetValue"); | |
MethodInfo SetContent = Tester.GetMethod(inputElement, "SetValue"); | |
MethodInfo Validate = Tester.GetMethod(inputElement, "Validate"); | |
try | |
{ | |
Console.WriteLine(GetContent.Invoke(inputElement, null)); | |
SetContent.Invoke(inputElement, new object[] {'T'}); | |
Console.WriteLine((bool)(Validate.Invoke(inputElement, null)) ? "true" : "false"); | |
Console.WriteLine("SUCCESS"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 3) | |
{ | |
int testMaxLength = 4; | |
String testValue = ""; | |
InputElement newInputElement = new InputElement(testMaxLength); | |
FieldInfo maxLength = Tester.GetField(newInputElement, "maxLength"); | |
FieldInfo content = Tester.GetField(newInputElement, "value"); | |
try | |
{ | |
if (maxLength != null) | |
{ | |
int value = int.Parse(maxLength.GetValue(newInputElement).ToString()); | |
if (value != testMaxLength) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
if (content != null) | |
{ | |
string value = content.GetValue(newInputElement).ToString(); | |
if (!(value.Equals(testValue))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch(Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 4) | |
{ | |
int testMaxLength = 4; | |
InputElement newInputElement = new InputElement(testMaxLength); | |
FieldInfo content = Tester.GetField(newInputElement, "value"); | |
MethodInfo GetContent = Tester.GetMethod(newInputElement, "GetValue"); | |
try | |
{ | |
if (content != null) | |
{ | |
string value = content.GetValue(newInputElement).ToString(); | |
if (!(value.Equals(GetContent.Invoke(newInputElement, null).ToString()))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 5) | |
{ | |
int testMaxLength = 4; | |
String testValue = "Test"; | |
InputElement newInputElement = new InputElement(testMaxLength); | |
MethodInfo SetContent = Tester.GetMethod(newInputElement, "SetValue"); | |
try | |
{ | |
SetContent.Invoke(newInputElement, new object[] {'T'}); | |
SetContent.Invoke(newInputElement, new object[] {'e'}); | |
SetContent.Invoke(newInputElement, new object[] {'s'}); | |
SetContent.Invoke(newInputElement, new object[] {'t'}); | |
FieldInfo content = Tester.GetField(newInputElement, "value"); | |
if (content != null) | |
{ | |
string value = content.GetValue(newInputElement).ToString(); | |
if (!(value.Equals(testValue))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newInputElement, new object[] {'s'}); | |
SetContent.Invoke(newInputElement, new object[] {'/'}); | |
FieldInfo content2 = Tester.GetField(newInputElement, "value"); | |
if (content2 != null) | |
{ | |
string value2 = content2.GetValue(newInputElement).ToString(); | |
if (!(value2.Equals(testValue))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 6) | |
{ | |
int testMaxLength = 4; | |
InputElement newInputElement = new InputElement(testMaxLength); | |
MethodInfo Validate = Tester.GetMethod(newInputElement, "Validate"); | |
MethodInfo SetContent = Tester.GetMethod(newInputElement, "SetValue"); | |
bool isValid; | |
bool expectedIsValid; | |
try | |
{ | |
isValid = bool.Parse(Validate.Invoke(newInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newInputElement, new object[] {'T'}); | |
SetContent.Invoke(newInputElement, new object[] {'e'}); | |
SetContent.Invoke(newInputElement, new object[] {'s'}); | |
SetContent.Invoke(newInputElement, new object[] {'t'}); | |
isValid = bool.Parse(Validate.Invoke(newInputElement, null).ToString()); | |
expectedIsValid = true; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newInputElement, new object[] {'s'}); | |
isValid = bool.Parse(Validate.Invoke(newInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 7) | |
{ | |
int testMaxLength = 4; | |
char[] testAllowedCharacters = new char[]{'t', 'E', 's', 'T'}; | |
String testValue = ""; | |
PasswordInputElement newPasswordInputElement = new PasswordInputElement(testMaxLength, testAllowedCharacters); | |
FieldInfo maxLength = Tester.GetField(newPasswordInputElement, "maxLength"); | |
FieldInfo content = Tester.GetField(newPasswordInputElement, "value"); | |
FieldInfo allowedCharacters = Tester.GetField(newPasswordInputElement, "allowedCharacters"); | |
try | |
{ | |
if (maxLength != null) | |
{ | |
int value = int.Parse(maxLength.GetValue(newPasswordInputElement).ToString()); | |
if (!(value == testMaxLength)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
if (content != null) | |
{ | |
string value = content.GetValue(newPasswordInputElement).ToString(); | |
if (!(value.Equals(testValue))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
if (allowedCharacters != null) | |
{ | |
char[] value = (char[]) allowedCharacters.GetValue(newPasswordInputElement); | |
if (!Enumerable.SequenceEqual(value, testAllowedCharacters)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch(Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 8) | |
{ | |
int testMaxLength = 4; | |
char[] testAllowedCharacters = new char[]{'t', 'E', 's', 'T'}; | |
PasswordInputElement newPasswordInputElement = new PasswordInputElement(testMaxLength, testAllowedCharacters); | |
MethodInfo Validate = Tester.GetMethod(newPasswordInputElement, "Validate"); | |
MethodInfo SetContent = Tester.GetMethod(newPasswordInputElement, "SetValue"); | |
bool isValid; | |
bool expectedIsValid; | |
try | |
{ | |
isValid = bool.Parse(Validate.Invoke(newPasswordInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newPasswordInputElement, new object[] {'t'}); | |
SetContent.Invoke(newPasswordInputElement, new object[] {'E'}); | |
SetContent.Invoke(newPasswordInputElement, new object[] {'s'}); | |
SetContent.Invoke(newPasswordInputElement, new object[] {'T'}); | |
isValid = bool.Parse(Validate.Invoke(newPasswordInputElement, null).ToString()); | |
expectedIsValid = true; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newPasswordInputElement, new object[] {'s'}); | |
isValid = bool.Parse(Validate.Invoke(newPasswordInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch(Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 9) | |
{ | |
int testMaxLength = 4; | |
char[] testAllowedCharacters = new char[]{'J', 'r', 'v', 'D'}; | |
String testValue = ""; | |
CustomPasswordInputElement newCustomPasswordInputElement = new CustomPasswordInputElement(testMaxLength); | |
FieldInfo maxLength = Tester.GetField(newCustomPasswordInputElement, "maxLength"); | |
FieldInfo content = Tester.GetField(newCustomPasswordInputElement, "value"); | |
FieldInfo allowedCharacters = Tester.GetField(newCustomPasswordInputElement, "allowedCharacters"); | |
try | |
{ | |
if (maxLength != null) | |
{ | |
int value = int.Parse(maxLength.GetValue(newCustomPasswordInputElement).ToString()); | |
if (!(value == testMaxLength)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
if (content != null) | |
{ | |
string value = content.GetValue(newCustomPasswordInputElement).ToString(); | |
if (!(value.Equals(testValue))) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
if (allowedCharacters != null) | |
{ | |
char[] value = (char[]) allowedCharacters.GetValue(newCustomPasswordInputElement); | |
if (!Enumerable.SequenceEqual(value, testAllowedCharacters)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch(Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 10) | |
{ | |
int testMaxLength = 4; | |
CustomPasswordInputElement newCustomPasswordInputElement = new CustomPasswordInputElement(testMaxLength); | |
MethodInfo Validate = Tester.GetMethod(newCustomPasswordInputElement, "Validate"); | |
MethodInfo SetContent = Tester.GetMethod(newCustomPasswordInputElement, "SetValue"); | |
bool isValid; | |
bool expectedIsValid; | |
try | |
{ | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'t'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'E'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'s'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'T'}); | |
isValid = bool.Parse(Validate.Invoke(newCustomPasswordInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'/'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'/'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'/'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'/'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'J'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'r'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'v'}); | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'D'}); | |
isValid = bool.Parse(Validate.Invoke(newCustomPasswordInputElement, null).ToString()); | |
expectedIsValid = true; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
SetContent.Invoke(newCustomPasswordInputElement, new object[] {'s'}); | |
isValid = bool.Parse(Validate.Invoke(newCustomPasswordInputElement, null).ToString()); | |
expectedIsValid = false; | |
if (!(expectedIsValid == isValid)) | |
{ | |
Console.WriteLine("FAILED"); | |
return; | |
} | |
Console.WriteLine("SUCCESS"); | |
} | |
catch(Exception) | |
{ | |
Console.WriteLine("FAILED"); | |
} | |
} | |
else if (code == 11) | |
{ | |
int testMaxLength = 4; | |
char[] testAllowedCharacters = new char[]{'t', 'E', 's', 'T'}; | |
PasswordInputElement newPasswordInputElement = new PasswordInputElement(testMaxLength, testAllowedCharacters); | |
CustomPasswordInputElement newCustomPasswordInputElement = new CustomPasswordInputElement(testMaxLength); | |
if (newPasswordInputElement is InputElement && | |
newCustomPasswordInputElement is InputElement && | |
newCustomPasswordInputElement is PasswordInputElement | |
) | |
{ | |
Console.WriteLine("SUCCESS"); | |
}else { | |
Console.WriteLine("FAILED"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WHAT ARE THE CHANGES? I have a lot of things to discuss, but the comparison is self-explanatory(?). TL;DR: possible inconsistencies(?) and confusion, content, GetContent, and SetContent. I tried to change the 'content' and other statements that referred to 'content' into 'value' which was stated in the instructions. I got confused because there is no instruction saying that we implement 'content' identifier properties, only value. So, I figured this was a mistake (?). After the revision, the code that I thought was the problem passed all the test cases.
Consistent Method and Field Access between the instructions, code, and the Tester.cs - It uses value consistently according to the instructions that were given. There are some cases where I get confused between the requirements of the Tester.cs and the instructions (or maybe I am just dumb).
Some Instances:
GUI Elements by CodeChum Admin
Graphical User Interface or GUI is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, instead of text-based UIs, typed command labels or text navigation. In this program, we will be implementing some of the different types of input elements.
First, implement the class InputElement.
It has the following properties:
It also implements these following methods:
[1] - Should this be [G]etValue and [S]etValue (Capital Letters), and not getValue and setValue? Since I can see that in the Tester.cs,
or, if there was a mistake in the labeling, it could be,
Oh, I found this as a recurring issue in my experience in CodeChum, where the instructions and the Tester class sometimes doesn't align.
[2] Should this be Validate()?
In the Tester.cs, every access of the validate() method is in capital V (This concerns me since it's case-sensitive). Snippets from the Tester.cs:
Another instance
In the Tester.cs (Line 88):
Console.WriteLine(Validate.Invoke(inputElement, null));
I think this should either be
Console.WriteLine((bool)(Validate.Invoke(inputElement, null)) ? "true" : "false");
, which is my workaround the Tester.cs class issue.Or change the expected output, for instance, in Tests Cases:
TEST CASE 4
Expected Output
Which type of InputElement do you want to create: 1
Enter max length: 3
Enter a character: a
Enter a character: b
Enter a character: c
Enter a character: d
Enter a character: @
Value is invalid
Enter code: 2
abcd
False <---- Instead of 'false'?
SUCCESS
TEST CASE 5
Expected Output
Which type of InputElement do you want to create: 2
Enter max length: 3
Enter allowed characters: C 0 d E c H u M
Enter a character: C
Enter a character: 0
Enter a character: d
Enter a character: E
Enter a character: @
Value is invalid
Enter code: 2
C0dE
False <---- Instead of 'false'?
SUCCESS
TEST CASE 6
Expected Output
Which type of InputElement do you want to create: 3
Enter max length: 4
Enter a character: C
Enter a character: o
Enter a character: D
Enter a character: y
Enter a character: @
Value is invalid
Enter code: 2
CoDy
False <---- Instead of 'false'?
SUCCESS
Why? I believe in C#, true and false when printed in the console, it will be capitalized [(Pascal Case Convention)]?(https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)
What do you think? Correct me if I am wrong. Thanks! I kind of enjoyed CodeChum. it's quite challenging. 😺