How do I have every test in class automatically tagged with a specific tag - scalatest

I am using the flatspec trait to create my tests and I would like to create a base class that would automatically tag any tests in that class with a particular tag.
For example, any tests in classes that inherit from the IntegrationTest class would automatically be appropriately tagged. So instead of:
class ExampleSpec extends FlatSpec {
"The Scala language" must "add correctly" taggedAs(IntegrationTest) in {
val sum = 1 + 1
assert(sum === 2)
}
I would like do this and still have the test tagged as an IntegrationTest
class ExampleSpec extends IntegrationSpec {
"The Scala language" must "add correctly" in {
val sum = 1 + 1
assert(sum === 2)
}
Thanks!

If you're willing to use a direct annotation on the test class, rather than a parent class, you can use the example at https://github.com/kciesielski/tags-demo. Adapted somewhat for your example, you need to declare a Java class:
package tags;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
#org.scalatest.TagAnnotation
#Retention(RUNTIME)
#Target({METHOD, TYPE})
public #interface MyAnnotation {
}
Then you use it to annotate the Scala test class:
#tags.MyAnnotation
class ExampleSpec extends FlatSpec {
"The Scala language" must "add correctly" in {
val sum = 1 + 1
assert(sum === 2)
}
You then have to use the actual string tags.MyAnnotation to specify the tag you want run (or ignored).
I tried to annotate a parent class instead, but I can't get it to work. I could imagine it being a significant problem for you or not, depending on what else you're trying to do.
Actually, the online doc for the org.scalatest.Tag class does a fair job of describing all this, although I say it after getting it to work by following the above project on GitHub..

Since ScalaTest 2.2.0 tags can be inherited (http://www.scalatest.org/release_notes/2.2.0).
Add #Inherited to your annotation definition.
package tags;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
**#Inherited**
#org.scalatest.TagAnnotation
#Retention(RUNTIME)
#Target({METHOD, TYPE})
public #interface RequiresIntegrationStuff {
}
Annotate your base spec.
#RequiresIntegrationStuff
class IntegrationSpec extends FlatSpec {}
Just use your base spec as a base class.
class ExampleSpec extends IntegrationSpec {
"The Scala language" must "add correctly" in {
val sum = 1 + 1
assert(sum === 2)
}
After that, ExampleSpec will be tagged as tags.RequiresIntegrationStuff.
You will find working project here: https://github.com/wojda/tags-demo (based on https://github.com/kciesielski/tags-demo from Spiro Michaylov's answer)

Related

mypy TypeVar include bound class as well as subclasses

class Super:
#classmethod
def instantiate(cls) -> What goes here?:
return cls()
class Sub(Super):
pass
class Sub2(Super):
pass
When writing typehints, what's the canonical way to say a method should return an instance of the super class or any of its subclasses. Closest I could find is TypeVar("Super", bound="Super") but this still raises an error for the Super class in mypy
here you can use typing.Type to type-hint cls like this:
from typing import TypeVar, Type
C = TypeVar("C")
class Super:
#classmethod
def instantiate(cls: Type[C]) -> C:
return cls()
class Sub(Super):
pass
class Sub2(Super):
pass
reveal_type(Sub.instantiate()) # note: Revealed type is 'tmp.Sub*'
reveal_type(Sub2.instantiate()) # note: Revealed type is 'tmp.Sub2*'

Base Class vs Extended Classes

I'm a new learner, I'm practicing the base and child classes. My question is how do we decide which class should be instantiated, extended or the Baseclass?
Thanks in advance
package MavenProject2Package2;
import org.testng.annotations.Test;
import MavenProject2Package.JavaTesting;
public class JavaTesting2 extends JavaTesting
{
#Test
public void f1()
{
JavaTesting a1 = new JavaTesting();
System.out.println(a1.msg);
JavaTesting2 a2 = new JavaTesting2();
System.out.println(a2.msg);
}
}
Base class - it's a class which you should be extending from. - eg - superclass.
In superclass you may put some general fields and methods, which are used across your web app. For example, locators for header as well as footer items, because they are the same for all the pages (mostly).

Kotlin object vs companion-object vs package scoped methods

I have written this methods in Kotlin and analysed the bytecode:
Situation 1
class A {
object b {
fun doSomething() {}
}
}
Situation 2
class A {
companion object b {
fun doSomething() {}
}
}
Situation 3
fun doSomething() {}
Bytecode Result
Situation 1: class Test$asb, public final doSomething()I
Situation 2: class Test$Companion, public final doSomething()I
Situation 3: class TestKt, public final static doSomething()I
My questions are:
I have an enum class, and I want to return an enum instace given an enum variable, for instance, findById (enum(id, color)). How would I do it? Companion Object? object?
It seems the only way to have a real static method is in package level, without class declaration. But that becomes a little bit too global. Is there any way to access it via: ClassName.staticMethod, staticMethod being really static.
Provide meaningfull examples of package declaration methods, companion object and object.
Context. I have been coding in Kotlin and I find it amazing. But sometimes I need to make a decision: for example, a heavy immutable property which in java I would declare as static final, but in Kotlin I find it hard to "find an equivalent".
If you have a function which performs some action closely related to a class but doesn't require a class instance, such as your findById example, you should put it in the companion object of the class.
If you want to expose a method as a static method to Java code, you can annotate it with the #JvmStatic annotation.
If a function does not require an instance of a class, then it is your design decision where to put it. Use package level if it is package-specific, use a class companion if it closely relets to the class (for example other classes in the package have similar functions).
Note that enum has several in-build properties and patterns:
enum class Colour(val value: Int) {
black(100), red(200), green(300)
}
fun colourById(id: Int) = Colour.values[id]
fun colourByValue(value: Int) = Colour.values.first {it.value == value}
fun colourByName(name: String) = Colour.valueOf(name)
I would suggest to develop voddan answer:
enum class Color {
RED,
BLUE,
GREEN;
companion object Utils {
fun findById(color: Color): Color {
return color;
}
}
}
And to test
#Test
fun testColor() {
println(Color.Utils.findById(Color.valueOf("RED")));
}

How to deal with spurious type script error: types of property $timeout of types base class and derived class are incompatible?

Since upgrading to TypeScript 1.6, I've been getting what appears to be a spurious error with all my ng.Types:
Typescript 1.6 class 'derivedClass' cannot extend class 'baseClass':
types of property $timeout of types base class and derived class are
incompatible
Here's the code sample:
module app.Test {
class derivedClass extends baseClass {
// Notice there's nothing in the derived class
}
class baseClass {
constructor(timeout: ng.ITimeoutService, val1: string, otherVal: ng.ILogService) {
var vm = this;
vm.$timeout = timeout;
vm.someValue = val1;
vm.otherValue = otherVal;
}
$timeout: ng.ITimeoutService; // angular.ITimeoutService works
someValue: string;
otherValue: ng.ILogService;
}
}
angular.ITimeoutService works, or adding import ng= angular; in my modules also works. This didn't used to be an issue in the previous versions of Typescript.
Is it best practice to repeat the alias (eg: import ng = angular) in each module before using it, even though that's already done in the angular.d.ts?
Is it best practice to repeat the alias (eg: import ng = angular) in each module before using it, even though that's already done in the angular.d.ts
NO. The error is happening probably because you have done this.
The code you have posted works fine by default:

CakePHP lazy loading fails with static access to class constants

In a CakePHP 2.2 app, I'm using class constants in a Model for some internal configuration. The following issue came up.
Short version:
Cake's lazy class loading will not be triggered by a static call to the Model class.
If the first access to a Model in a Controller is
MyModel::SOME_CONST // fails
the class will be unknown. If any instance of the class is used before, it's fine:
$this->MyModel->something();
MyModel::SOME_CONST // works
Not knowing about the details of the lazy loading implementation:
Question: Is this something that is impossible to fix? If so, why? How do I then best work around it in my App myself (wrap consts in a function)? Or is there a chance to improve the lazy loading so that it works with static access, too?
Long version with code:
In order to test the different cases, I made a small test App with 1 Model and 1 Controller:
Model/Post.php:
<?php
class Post extends AppModel {
public $useTable = false; // Don't bother with a DB
const FOO = "foo";
public $bar = "bar";
}
Controller/PostsController.php:
<?php
class PostsController extends AppController {
public function constant() {
debug(Post::FOO);
}
public function variable() {
debug($this->Post->bar);
}
public function variableFirst() {
debug($this->Post->bar);
debug(Post::FOO);
}
}
Accessing the three controller actions through the browser, the different cases can now be tested.
1) accessing the Model constant (at /posts/constant):
Error: Class 'AppModel' not found
2) accessing the Model variable (at /posts/variable):
'bar'
3) accessing the Model constant AFTER a variable (at /posts/variable):
'bar'
'foo'
lazyloading works with normal class calls as well as static calls IF you correctly approach it.
Correctly means, that you always have to App::uses() all used classes at the top of your file
for AppModel in a model file:
App::uses('AppModel', 'Model');
class Post extends AppModel {}
see the core files for details.

Resources