What is the easiest way to run the same test and the same suite or class n times in a row using kotest framework?
I'm not sure if you can run the same test class n times, but you can configure all tests in the class to run n times by setting the default config for the class like so:
class RepeatAllTests : FunSpec() {
override fun defaultConfig() = TestCaseConfig(invocations = 5)
init {
test("repeat us") { 0 shouldBe 0 }
}
}
Or you can mark a specific test to be repeated like so:
class RepeatATest : FunSpec() {
init {
test("repeat me").config(invocations = 5) { 0 shouldBe 1 }
}
}
Also see the docs regarding invocations here: http://kotest.io/docs/framework/testcaseconfig.html
Related
i am wondering is there some syntax in Kotlin for declaring a variable which will be equal to a function which takes several parameters 1 of which is said variable?
class Player(var deck: MutableList<String> = Deck().deck) {
var playerHand = Deck().deal(deck, playerHand, 6)
}
class Deck {
var deck = mutableListOf<String>()
fun deal( from: MutableList<String>, to: MutableList<String>, num: Int ){
var temp = from.slice(0 .. num).toMutableList()
to.addAll(temp)
from.removeAll(temp)
}
}
So i basically want to transfer N amount of cards from a deck inside a Deck class to a variable playerHand in Player class. I know there are numerous other ways of doing it, i just wanted to know is there a way to create a var and assign a value to it using a function that takes that var as a parameter..
I stared at your code for a while and finally realized what you're asking, I think: You want playerHand to be a MutableList whose initial values come from calling Deck().deal(). Like #broot says in the comment, the standard way to do this is to use also. I'm also assuming it's some kind of typo that you are instantiating a new Deck instance to deal from, when you already have a deck property in your class. However, that property should probably be a Deck instance directly so you still have access to the Deck functionality that goes with it.
You might consider making your Deck class implement MutableList using a delegate to simplify how it is referenced and used. Now you can directly use existing MutableList functions like shuffle() directly on your Deck instance. And I would use LinkedList specifically as the delegate because it is more efficient than the default returned by mutableListOf at removing values from the top.
I would also make the deal function simply a dealTo function because it should be assumed a Deck is only going to deal from itself.
Putting that all together:
class Player(var deck: Deck = Deck()) {
val playerHand = mutableListOf<String>().also {
deck.dealTo(it, 6)
}
}
class Deck: MutableList<String> by LinkedList<String>() {
fun dealTo(to: MutableList<String>, num: Int) {
repeat(num) {
to += removeFirst()
}
}
}
How to assign the explicit reset to a register. When RegInit() is used, global reset signal assign to it . However if you want to drive the reset signal through custom logic in a module then how can we avoid implicit reset.
for example
...
state_reg = RegInit(st_reset)
when( reset_i) {
state_reg:= st_reset
}.elsewhen(error_s) {
state_reg := st_error
}.otherwise {
state_reg:= next_state_reg_s
}
....
Can anyone explain how to control reset logic.
You need to declare the register in the scope of another clock. Something like
val reg2 = withClock(clock2) { RegInit(0.U(8.W)) }
See the following chisel3 tests multiclock example for the full implementation.
I'm trying to make a custom GTK widget in Vala, but I'm already failing at the very first basic attempt, so I'd like some help in knowing where I'm going wrong. I feel like I must be missing something painstakingly obvious, but I just can't see it.
I have three files with the following contents:
start.vala:
using Gtk;
namespace WTF
{
MainWindow main_window;
int main(string[] args)
{
Gtk.init(ref args);
main_window = new MainWindow();
Gtk.main();
return 0;
}
}
main_window.vala:
using Gtk;
namespace WTF
{
public class MainWindow : Window
{
public MainWindow()
{
/* */
Entry entry = new Entry();
entry.set_text("Yo!");
this.add(entry);
/* */
/*
CustomWidget cw = new CustomWidget();
this.add(cw);
/* */
this.window_position = WindowPosition.CENTER;
this.set_default_size(400, 200);
this.destroy.connect(Gtk.main_quit);
this.show_all();
}
}
}
custom_widget.vala:
using Gtk;
namespace WTF
{
public class CustomWidget : Bin
{
public CustomWidget()
{
Entry entry = new Entry();
entry.set_text("Yo");
this.add(entry);
this.show_all();
}
}
}
As you can see, in main_window.vala, I have two sets of code. One that adds the Entry widget directly, and one that adds my custom widget. If you run the one that adds the Entry widget directly, you get this result:
If you run the one with the custom widget, however, you get this result:
Just for the record, this is the complication command I use:
valac --pkg gtk+-2.0 start.vala main_window.vala custom_widget.vala -o wtf
EDIT:
Following user4815162342's suggestion, I implemented the size_allocate method on my custom Bin widget, like so:
public override void size_allocate(Gdk.Rectangle r)
{
stdout.printf("Size_allocate: %d,%d ; %d,%d\n", r.x, r.y, r.width, r.height);
Allocation a = Allocation() { x = r.x, y = r.y, width = r.width, height = r.height };
this.set_allocation(a);
stdout.printf("\tHas child: %s\n", this.child != null ? "true" : "false");
if (this.child != null)
{
int border_width = (int)this.border_width;
Gdk.Rectangle cr = Gdk.Rectangle()
{
x = r.x + border_width,
y = r.y + border_width,
width = r.width - 2 * border_width,
height = r.height - 2 * border_width
};
stdout.printf("\tChild size allocate: %d,%d ; %d, %d\n", cr.x, cr.y, cr.width, cr.height);
this.child.size_allocate(cr);
}
}
It writes the following in the console:
Size_allocate: 0,0 ; 400,200
Has child: true
Child size allocate: 0,0 ; 400, 200
And the window renders thusly:
GtkBin is an abstract single-child container, typically intended to decorate the child widget in some way, or change its visibility or size. Without some added value, a single-child container would be indistinguishable from the widget it contains and therefore not very useful.
Since GtkBin doesn't know what kind of decorations you will draw around the child, it expects you to implement your own size_allocate. A simple implementation is available in gtk_event_area_size_allocate, a more complex one in gtk_button_size_allocate.
This answer shows a minimal size_allocate implementation in PyGTK which should be straightforward to port to Vala. If you do anything more complex than that, you will need to also implement expose, and possibly other methods, but this will get you started.
If you take the numPanels as a fixed value of e.g. 20, it's really a nice and groovy swinging.
But I try since two days to add more or less panels with a groovy.swing.Spinner dynamically (my program will be used to connect to different databases and exchange values from different db-tables). I tried with binding, revalidating, repainting - but I can't bring the closure to take the new value from the spinner...
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.BoxLayout as BXL
import javax.swing.SpinnerNumberModel
int numPanels = 2
swing = new SwingBuilder()
def setPanelAmount = swing.action(name:'Amount of Panels in vbox-element', closure: this.&setPanelAmount )
frame = swing.frame(title:'test', pack:true, visible:true, defaultCloseOperation:WC.HIDE_ON_CLOSE) {
panel(id:'mainPanel'){
scrollPane( verticalScrollBarPolicy:JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ) {
vbox {
(1..numPanels).each { num ->
def panelID = "panel$num"
def pane = panel( alignmentX:0f, id:panelID, background:java.awt.Color.GREEN ) {
label('description')
textField( id: "description$num", text:panelID, columns: 70 )
button( id: "buttonpanel$num", text:panelID, actionPerformed:{
swing."$panelID".background = java.awt.Color.RED
} )
}
}
}
}
boxLayout(axis: BXL.Y_AXIS)
panel(id:'secondPanel' , alignmentX: 0f){
hbox(){
label 'Change amount of panels:'
hstrut(10)
spinner(id: 'numPanelSpinner', stateChanged: this.&setPanelAmount, model: new SpinnerNumberModel(2, 2, 10, 1))
hstrut(50)
button('Quit', actionPerformed:{
frame.visible = false
})
}
}
}
}
frame.size = [ frame.width, 600 ]
def setPanelAmount(event) {
numPanels = swing.numPanelSpinner.getValue()
}
It has to be combined with the following code - but unfortunately, it's not working with an included closure - at least as far as I know now...
import groovy.swing.*
import javax.swing.*
import java.awt.*
import java.awt.BorderLayout as BL
new SwingBuilder().edt {
frame(defaultCloseOperation: JFrame.EXIT_ON_CLOSE, visible: true, size: [600,500]) {
panel(id:'main') {
panel {
button(name:'x', action: action(name:'add', closure:{p.add(label('new')); p.revalidate()}))
button(action: action(name:'remove', closure:{p.removeAll();p.revalidate();scroll.repaint()}))
}
panel() {
scrollPane(id:'scroll',preferredSize: [200,200], constraints: context.CENTER) {
panel(id:'p') {
checkBoxList(listData: (1..20).collect([]){"Option $it"} as Object[])
}
}
}
}
}
}
BTW.: do you like to improve in programming groovy, too?
I would be glad to start a git-based "engineering" with my program.
This program will compare a user given amount of lists with articles coming from one or more databases or excel-files, save them and every result in PostgreSQL or MSSQL.
Every list may have zero, one or more articles listed identified by their article-numbers, with varying amounts in each entity.
And the user can - guided by a GUI - decide in which order to add or subtract the entities from another. The other (zero to infinite) attributes beside of article-number and amount will be carried through the lists.
Not too complicated but for a first project...
Lots of references for creating lookups out there, but all seem to draw their values from a query.
I want to add a lookup to a field that will add items from a list of values that do not come from a table, query, or any other data source.
Such as from a string: "Bananas, Apples, Oranges"
..or a container ["Bananas", "Apples", "Oranges"]
Assume the string/container is a dynamic object. Drawing from an static enum is not a choice.
Is there a way to create lookups on the fly from something other than a data source?
Example code would be a great help, but I'll take hints as well.
There is the color picker.
Also in the Global you will find pickXxxx such as pickList.
There are others, pickUser, pickUserGroup etc.
Take a look on the implementation. I guess they build a temporary table then displays that. Tables are great!
Update:
To go on you own follow the rules.
For the advanced user, see also: Lookup form returning more than one value.
public void lookup()
{
SysTableLookup sysTableLookup;
TmpTableFieldLookup tmpTableFieldLookup;
Enumerator en;
List entitylist = new list(types::String);
entitylist.addend("Banana");
entitylist.addend("Apple");
en = entityList.getEnumerator();
while (en.moveNext())
{
tmpTableFieldLookup.TableName = en.current();
tmpTableFieldLookup.insert();
}
sysTableLookup = SysTableLookup::newParameters(tableNum(tmpTableFieldLookup), this);
sysTableLookup.addLookupfield(fieldNum(TmpTableFieldLookup, TableName));
//BP Deviation documented
sysTableLookup.parmTmpBuffer(tmpTableFieldLookup);
sysTableLookup.performFormLookup();
}
The above code helps in displaying strings as lookup.
I'm also guessing there's no way to perform a lookup without a table. I say that because a lookup is simply a form with one or more datasources that is displayed in a different way.
I've also blogged about this, so you can get some info on how to perform a lookup, even with a temporary table, here:
http://devexpp.blogspot.com.br/2012/02/dynamics-ax-custom-lookup.html
Example from global::PickEnumValue:
static int pickEnumValue(EnumId _enumId, boolean _omitZero = false)
{
Object formRun;
container names;
container values;
int i,value = -1,valueIndex;
str name;
#ResAppl
DictEnum dictEnum = new DictEnum(_enumId);
;
if (!dictEnum)
return -1;
for (i=1;i<=dictEnum.values();i++)
{
value = dictEnum.index2Value(i);
if (!(_omitZero && (value == 0)))
{
names += dictEnum.index2Label(i);
values += value;
}
}
formRun = classfactory.createPicklist();
formRun.init();
formRun.choices(names, #ImageClass);
formRun.caption(dictEnum.label());
formRun.run();
formRun.wait();
name = formRun.choice();
value = formRun.choiceInt();
if (value>=0) // the picklist form returns -1 if a choice has not been made
{
valueIndex = -1;
for (i=1;i<=conLen(names);i++)
{
if (name == conPeek(names,i))
{
valueIndex = i;
break;
}
}
if (valueIndex>=0)
return conPeek(values,valueIndex);
}
return value;
}
It isn't the most graceful solution, but this does work, and it doesn't override or modify any native AX 2012 objects:
Copy the sysLookup form from AX2009 (rename it) and import it into AX 2012.
We'll call mine myLookupFormCopy.
I did a find/replace of "sysLookup" in the XPO file to rename it.
Create this class method:
public static client void lookupList(FormStringControl _formStringControl, List _valueList, str _columnLabel = '')
{
Args args;
FormRun formRun;
;
if (_formStringControl && _valueList && _valueList.typeId() == Types::String)
{
args = new Args(formstr(myLookupFormCopy));
args.parmObject(_valueList);
args.parm(_columnLabel);
formRun = classFactory.formRunClass(args);
_formStringControl.performFormLookup(formRun);
}
}
In the lookup method for your string control, use:
public void lookup()
{
List valueList = new List(Types::String);
;
...build your valueList here...
MyClass::lookupList(this, valueList, "List Title");
super();
}