Skip to content

Instantly share code, notes, and snippets.

View automationhacks's full-sized avatar

Gaurav Singh automationhacks

View GitHub Profile
@automationhacks
automationhacks / duck_typing.py
Created May 27, 2018 05:53
Illustrates duck typing in the simplest way
class Duck:
def quack(self):
print("Quacked")
class AnotherDuck:
def quack(self):
print("Louder Quack")
class Eagle:
def fly(self):
@automationhacks
automationhacks / Person.java
Created June 16, 2019 12:02
Simple POJO in java for a Person
package _01_bean;
public class Person {
private final String name;
private final Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
@automationhacks
automationhacks / Person.kt
Created June 16, 2019 12:16
Simple POJO in Kotlin
package _01_bean
class PersonKT(
val name: String,
val age: Int)
@automationhacks
automationhacks / Person.kt
Created June 16, 2019 12:17
Main func using Person.kt
fun main(args : Array<String>) {
val person = PersonKT("Jack", 30)
println(person.name)
println(person.age)
}
@automationhacks
automationhacks / NameParser.kt
Created June 16, 2019 12:18
Example KT class to show use of data classes
package _02_dataClasses
import org.testng.Assert
fun parseName(name : String): List<String> {
val space = name.indexOf(' ')
return listOf(
name.substring(0, space),
name.substring(space + 1)
)
@automationhacks
automationhacks / NameParserV2.kt
Created June 16, 2019 12:20
Refactored Name Parser file with use of Data classes
package _02_dataClasses
import org.testng.Assert
class FullName(val first: String,
val last: String)
fun parseName(name: String): FullName {
val space = name.indexOf(' ')
return FullName(
@automationhacks
automationhacks / Functions.kt
Created June 16, 2019 12:21
Initial Example of show use of functions and available features
package _03_functions
class StringUtils {
fun getFirstWord(word: String, separator: String): String {
return word.split(separator)[0]
}
fun getFirstWord(word: String) = getFirstWord(word, " ")
}
@automationhacks
automationhacks / FunctionV2.kt
Created June 16, 2019 12:23
Moved function outside class as first class object and use of default parameters
package _03_functions
fun getFirstWord(word: String, separator: String = " "): String {
return word.split(separator)[0]
}
fun main() {
val first = getFirstWord("Jane Doe")
println("First word: $first")
@automationhacks
automationhacks / ExtensionFunction.kt
Created June 16, 2019 12:24
Refactoring above code to use Extension functions
package _03_functions
fun String.getFirstWord(separator: String = " "): String {
return this.split(separator)[0]
}
fun main() {
val first = "Jane Doe".getFirstWord()
println("First word: $first")
@automationhacks
automationhacks / FunctionFinal.kt
Created June 16, 2019 12:25
Final code with all refactorings done
package _03_functions
fun String.getFirstWord(separator: String = " "): String {
return this.split(separator)[0]
}
// Example of extension properties
// In this case on top of a String class
val String.lastWord: String