How to change system date time before launch test [XCTest, swift] - xctest

I have no idea How to change system date time before launch test on real devices or simulator. Could you help me?

I don't think you can actually change the system time. What you can do is provide your own class that returns dates and use it everywhere in place of the Date class. In your custom class you will be able to choose to return either your test time or system time, depending on if you are testing or not.
If you do this, your in-app code would read something like let now = CustomDate() instead of let now = Date().
This question does already have an answer for Objective-C here: Set iPhone date/time within app for testing purposes?.

Here is a simple way to do this in Swift.
class CustomDate {
static var dateOverride: Date?
static func get() -> Date {
return dateOverride ?? Date()
}
}
Then instead of using Date(), use CustomDate.get() in your app.
In your test code, set CustomDate.dateOverride to anything you like and you should be able to simulate system date changes in your app. However, in your app code, never set dateOverride to avoid unexpected behavior.

Related

AST and Language Service Plugin

I would like to create a vscode extension which can help with unit testing and "live" debugging.
Lets say you have the following code:
/*1*/ export class Test {
/*2*/ public add(a: any, b: any): any {
/*3*/ return a + b;
/*4*/ }
/*5*/ }
Live debugging:
New option in the context menu to test the add function (e.g. something similar to current Peek Definition) where you can provide the function input and check the output in a new smaller editor.
Unit test generation:
After you wrote an input and checked the output you can simply generate a new unit test from it.
Now I'm only at the beginning of this project but it seems like getting type information is much more complicated than I expected.
First I would like to get some simple type information (based on cursor position) e.g.:
Line 1 is a class
Line 2-4 is a function
Line 5 is a class
After hours of Google search I think what I have to do is to implement my own TypeScript Language Service Plugin and use it somehow in my extension.
My questions:
Is there any extension similar to my idea which I could use?
Is there an easier way to get the type information I need?
Am I on a right track at all?
Do you have some material about how to use this language service plugin from extension?
P.S. I know that it won't be easy (e.g. several states with different visibility), but good practice for a home project.

"python function decorator" for objective-c to change a method's behavior

I want to modify the behavior of some function without being the author of that function. What I control is that I can ask the author to follow some pattern, e.g. use a base class, use a certain decorator, property etc.
If in python, I would use a decorator to change the behavior of a method.
As an example, My goal: Improve code coverage by automatically testing over multiple input data.
Pseudo code:
#implementation SomeTestSuiteClass
// If in python, I would add a decorator here to change the behavior of the following method
-(void)testSample1 {
input = SpecialProvider();
output = FeatureToTest(input);
SpecialAssert(output);
}
#end
What I want: During test, the testSample1 method will be called multiple times. Each time, the SpecialProvider will emit a different input data. Same for the SpecialAssert, which can verify the output corresponding to the given input.
SpecialProvider and SpecialAssert will be API under my control/ownership (i.e. I write them).
The SomeTestSuiteClass together with the testSample1 will be written by the user (i.e. test writer).
Is there a way for Objective-C to achieve "what I want" above?
You could mock objects and/or its methods using objective-c runtime or some third party frameworks. I discourage it though. That is a sign of poor architecture choices in the 1st place. The main problem in your approach are hidden dependencies in your code directly referencing
SpecialProvider & SpecialAssert symbols directly.
A much better way to this would be like this:
-(void)testSample1:(SpecialProvider*)provider assert:(BOOL (^)(parameterTypes))assertBlock {
input = provider;
output = FeatureToTest(input);
if (assertBlock != nil) {
assertBlock(output);
}
}
Since Objective-c does not support default argument values like Swift does you could emulate it with:
-(void)testSample1 {
[self testSample1:DefaultSpecialProvider() assert:DefaultAssert()];
}
not to call the explicit -(void)testSample1:(SpecialProvider*)provider assert:(BOOL (^)(parameterTypes))assertBlock all the time, however in tests you would always use the explicit 2 argument variant to allow substituting the implementation(s) not being under test.
Further improvement idea:
Put the SpecialProvider and SpecialAssert behind protocols(i.e. equivalent of interfaces in other programming languages) so you can easily exchange the implementation.

Spock Mock internal Date object

I am returning a string from the groovy utility class. The method is
return "${new SimpleDateFormate("dd_MM_yyyy").format(new Date())}"
I want to test this method with my date. Can I mock the new Date() as 2016-01-14.
If it is possible, how to implement it.
Why not change the method to take a date as a parameter, then it's easily testable?
Though you appear to be testing that simple date format works. I believe oracle have you covered

Custom parameters in Pentaho dashboards

Custom parameters in a CDE/CTools dashboard are great for defaulting initial values of parameters, e.g. setting a date parameter to today. i.e. the parameter looks like:
function() {
// some code
return val
}
However there is an issue with them. The first time you access a "custom parameter" in code, it is a function not a string. So you have to use:
paramName()
To get its value.
Once the end user selects a value then you have to use
paramName
This is really awkward in complicated dashboards with lots of prompts. Is there a better way this can be done? (Perhaps there is something in javascript I'm missing to help here?)
OK, there is a solution, but I dont like it!
First; Move all the init code into named procedures e.g.
function monthInit() {
return "june";
}
Then in the custom parameter for month, just say:
monthInit();
That way the custom parameter is always a string, and never starts off as a function.
Not ideal though because then all your init code is in a separate bit of js.

Use UUID as action parameters in Play Framework

I'd like to use UUID as action parameters. However, unless I use .toString() on the UUID objects when generating the action URL's, Play seems to serialize the object differently; Something like this: referenceId.sequence=-1&referenceId.hashCode=1728064460&referenceId.version=-1&referenceId.variant=-1&referenceId.timestamp=-1&referenceId.node=-1
However, using toString "works", but when I redirect from one action to another by simply invoking the method directly, there's no way I can call toString, as the method expects a UUID. Therefore it gives me the representation shown above.
Is there any way I can intersect the serialization of a certain type?
aren't you able to just use string in your action parameter? you know that this string is an UUID, so you can always recreate UUID from it. Maybe this is not the solution for you but that's my first thought. As far as I know play serializes objects like that when passing them trough paremeters.
If this does not work for you try finding something here: http://www.playframework.org/documentation/1.2.4/controllers
I found a way to do this, but right now it means hacking a part of the frameworks code itself.
What you basically need is a TypeBinder for binding the value from the String to the UUID
and a small code change in
play/framework/src/play/data/binding/Unbinder.java
if (!isAsAnnotation) {
// We want to use that one so when redirecting it looks ok. We could as well use the DateBinder.ISO8601 but the url looks terrible
if (Calendar.class.isAssignableFrom(src.getClass())) {
result.put(name, new SimpleDateFormat(I18N.getDateFormat()).format(((Calendar) src).getTime()));
} else {
result.put(name, new SimpleDateFormat(I18N.getDateFormat()).format((Date) src));
}
}
}
//here's the changed code
else if (UUID.class.isAssignableFrom(src.getClass()))
{
result.put(name, src.toString());
}
else {
// this code is responsible for the behavior you're seeing right now
Field[] fields = src.getClass().getDeclaredFields();
for (Field field : fields) {
if ((field.getModifiers() & BeanWrapper.notwritableField) != 0) {
// skip fields that cannot be bound by BeanWrapper
continue;
}
I'm working with the framework authors on a fix for this. will come back later with results.
if you need this urgently, apply the change to the code yourself and rebuild the framework by issuing
ant
in the playframework/framework
directory.