Clang-Format. How to make function parameters to be on their own line if line is too long? - clang-format

Now I have that behaviour:
void methodName(int arg1,
int arg2,
int arg3) {
...
}
I want:
void methodName(
int arg1,
int arg2,
int arg3
) {
...
}
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Related

Dart Null Safety giving errors for typedef Function

I have an example that works in dart before null safety that doesn't after upgrading to Null safety. I can't figure out what's going on.
It gives me the error
A value of type 'dynamic Function()?' can't be returned from the method 'doSomething' because it has a return type of 'dynamic Function()'
But I didn't define anything as Nullable.
typedef RandomFunction = Function();
class RegisteredFunctions {
Map<String, RandomFunction> types = {};
static final RegisteredFunctions _registry = RegisteredFunctions._init();
RegisteredFunctions._init() {}
factory RegisteredFunctions() {
return _registry;
}
void registerFunction(String name, RandomFunction func) {
types[name] = func;
}
RandomFunction doSomething(String id) => types[id]; //<---- gives error
}
void doStuff(){
print('Doing Something');
}
void main() {
RegisteredFunctions functions = RegisteredFunctions();
functions.registerFunction('func1', doStuff);
functions.doSomething('func1')();
}
For anyone else trying to figure it out.. this fixed it.
typedef RandomFunction = Object? Function();
class RegisteredFunctions {
Map<String, RandomFunction> types = {};
static final RegisteredFunctions _registry = RegisteredFunctions._init();
RegisteredFunctions._init() {}
factory RegisteredFunctions() {
return _registry;
}
void registerFunction(String name, RandomFunction func) {
types[name] = func;
}
// RandomFunction doSomething(String id) => types[id]; // <----- Doesn't work
// RandomFunction doSomething(String id) => types[id]!; // <----- Works
RandomFunction doSomething(String id) { // <----- Works better
RandomFunction? func = types[id];
if (func != null) {
return func;
} else {
return (){};
}
}
}
void doStuff(){
print('Doing Something');
}
void doOtherStuff(){
print('Doing Something else');
}
void main() {
RegisteredFunctions functions = RegisteredFunctions();
functions.registerFunction('func1', doStuff);
functions.registerFunction('func2', doOtherStuff);
functions.doSomething('func1')();
functions.doSomething('func2')();
}

Why TextWatcher is not working in custom Edittext in android?

I am working on custom edittext but i am little stuck in one thing, i found TextWatcher is not working in custom edittext.
public class InputValidation extends EditText {
public InputValidation(Context context) {
super(context);
}
public InputValidation(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InputValidation(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void addTextChangedListener(android.text.TextWatcher watcher) {
super.addTextChangedListener(new TextWatcherDelegator());
}
public class TextWatcherDelegator implements android.text.TextWatcher {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
android.util.Log.d("TextWatcher", " beforeTextChanged :: " + charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(android.text.Editable editable) {
android.util.Log.d("TextWatcher", " afterTextChanged :: " + editable.toString());
}
}
}
XML layout
<com.example.inputvalidation.InputValidation
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="Fullname"
android:singleLine="true"
android:textSize="20sp"/>
Above this code it's not at all calling TextWatcher states, please kindly go through my code and suggest me some solution.
Remove this, it's useless:
#Override
public void addTextChangedListener(android.text.TextWatcher watcher) {
super.addTextChangedListener(new TextWatcherDelegator());
}
Then, add/remove the TextWatcher properly, for example in onAttachedToWindow/onDetachedFromWindow methods:
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
addTextChangedListener(textWatcher);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
removeTextChangedListener(textWatcher);
}
Also, textWatcher should be an object, so it can be instanced from the TextWatcherDelegator class:
TextWatcherDelegator textWatcher = new TextWatcherDelegator();
Or directly from TextWatcher (which is better, if there's no other usages for TextWatcherDelegator):
public TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
android.util.Log.d("TextWatcher", " beforeTextChanged :: " + charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(android.text.Editable editable) {
android.util.Log.d("TextWatcher", " afterTextChanged :: " + editable.toString());
}
}

How to get new edit Text on Enter Key pressed event in Dialogbox using Android programing? Can you provide sample code for that?

I had tried through below code but not getting key event.
editText1.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(keyCode==KeyEvent.KEYCODE_ENTER))
{
editText1.clearFocus();
editText2.requestFocus();
return true;
}
return false;
}
});
use event.getKeyCode() instead of just keyCode
editText1.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(event.getKeyCode()==KeyEvent.KEYCODE_ENTER))
{
editText1.clearFocus();
editText2.requestFocus();
return true;
}
return false;
}
});
I would use Textwatcher instead:
try this
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n"))
{
editText1.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
editText1.clearFocus();
editText2.requestFocus();
}
}
});

Dart How to mock a procedure

How do I go about mocking a procedure (as apposed to a function see here)
For example, given the following typedef and procedure,
typedef int Adder(int a, int b);
int useAdder(Adder adder) {
return adder(1, 2);
}
How could you write a mock that would allow you to test that the userAdder procedure called you mocked function?
This was my attempt, but it fails with the message that test failed: Caught The null object does not have a method 'call'.
class MyMock extends Mock {
MyMock(){
when(callsTo('call')).alwaysCall(this.foo);
}
int foo(int a, int b) => a+b;
}
void main() {
test("bb", () {
var mockf = new MyMock();
expect(useAdder( mockf.call), 3);
mockf.getLogs(callsTo('call', 1, 2)).verify(happenedOnce);
});
}
If I change
expect(useAdder( mockf.call), 3);
to
expect(useAdder( mockf.foo), 3);
the method call does not appear in the log
My attempt
import 'package:unittest/unittest.dart';
import 'package:mock/mock.dart';
typedef int Adder(int a, int b);
int useAdder(Adder adder) {
return adder(1, 2);
}
class MyMock extends Mock {
MyMock(){
when(callsTo('call')).alwaysCall(this.foo);
}
int foo(int a, int b) => a+b;
int call(int a, int b) => super.call(a, b);
}
void main() {
test("bb", () {
var mockf = new MyMock();
expect(useAdder(mockf as Adder), 3);
mockf.getLogs(callsTo('call', 1, 2)).verify(happenedOnce);
});
}
It seems the call method has to actually exist to make MyMock being accepted as Adder.
I updated my code with the core idea from the Günter Zöchbauer's solution.
library functionMockTest;
import "package:unittest/unittest.dart";
import "package:mock/mock.dart";
import "dart:math" as math;
typedef int BinaryIntToInt(int a, int b);
typedef double BinaryIntToDouble(int a, int b);
typedef int DoubleIntToInt(double a, int b);
int useBinaryIntToInt(BinaryIntToInt adder) => adder(1, 2);
void main() {
test('Mock [min] function from the "dart:math"', () {
var min = new FunctionMock(math.min);
expect(min(2,1), 1);
expect(min(1,2), 1);
min.calls('call', 1, 2).verify(happenedOnce);
});
test("Function Mock", () {
var adder2 = new FunctionMock<BinaryIntToInt>((a,b) => a + b);
var adder3 = new FunctionMock((a,b,c) => a + b + c);
expect(adder2 is Function, true);
expect(useBinaryIntToInt(adder2), 3);
expect(adder3(1,2,3), 6);
adder2.calls('call', 1, 2).verify(happenedOnce);
});
group("Type check on function mocks:", (){
test("Should throw [TypeError] \n "
"if function has wrong number of arguments", () {
TypeError error;
try {
var adder3 = new FunctionMock<BinaryIntToInt>((a,b,c) => a + b + c);
}
catch(exception) {
expect(exception is TypeError, true);
error = exception;
}
expect(error != null, true);
});
test("Should throw [TypeError] \n "
"if function has wrong type of arguments", () {
TypeError error;
try {
var adder3 = new FunctionMock<BinaryIntToInt>((double a,b) => 10);
}
catch(exception) {
expect(exception is TypeError, true);
error = exception;
}
expect(error != null, true);
});
test("Doesn't throw on typedef mismatch \n"
"without type annotation", () {
BinaryIntToDouble foo = (c,d) => c / d;
DoubleIntToInt bar = (c,d) => c + d;
var wrongTypedefReturnType = new FunctionMock<BinaryIntToInt>(foo);
var wrongTypedefArgumentType = new FunctionMock<BinaryIntToInt>(bar);
wrongTypedefReturnType(1.1,2.1);
wrongTypedefArgumentType(1.1,2.1);
});
test("Throws with type annotation", () {
double foo_ (int c, int d) => c / d;
BinaryIntToDouble foo = foo_;
int bar_ (double c, int d) => 10;
DoubleIntToInt bar = bar_;
TypeError returnTypeError, argumentTypeError;
try {
var wrongTypedefReturnType = new FunctionMock<BinaryIntToInt>(foo);
}
catch(exception) {
expect(exception is TypeError, true);
returnTypeError = exception;
}
try {
var wrongTypedefArgumentType = new FunctionMock<BinaryIntToInt>(bar);
}
catch(exception) {
expect(exception is TypeError, true);
argumentTypeError = exception;
}
expect(returnTypeError != null, true);
expect(argumentTypeError != null, true);
});
}
);
}
class _Sentinel {
const _Sentinel();
}
const NO_ARG = const _Sentinel();
class FunctionMock<FunctionTypedef> extends Mock implements Function{
final FunctionTypedef _callable;
FunctionMock._internal(FunctionTypedef function) : _callable = function {
when(callsTo('call')).alwaysCall(_callable);
}
//Place to 'shovel in' black magic if needed.
factory FunctionMock(FunctionTypedef function){
return new FunctionMock._internal(function);
}
call([arg0 = NO_ARG,
arg1 = NO_ARG,
arg2 = NO_ARG,
arg3 = NO_ARG,
arg4 = NO_ARG,
arg5 = NO_ARG,
arg6 = NO_ARG,
arg7 = NO_ARG,
arg8 = NO_ARG,
arg9 = NO_ARG]) {
if (identical(arg0, NO_ARG)) return super.call();
if (identical(arg1, NO_ARG)) return super.call(arg0);
if (identical(arg2, NO_ARG)) return super.call(arg0, arg1);
if (identical(arg3, NO_ARG)) return super.call(arg0, arg1, arg2);
if (identical(arg4, NO_ARG)) return super.call(arg0, arg1, arg2, arg3);
if (identical(arg5, NO_ARG)) return super.call(arg0, arg1, arg2, arg3, arg4);
if (identical(arg6, NO_ARG)) return super.call(arg0, arg1, arg2, arg3, arg4,
arg5);
if (identical(arg7, NO_ARG)) return super.call(arg0, arg1, arg2, arg3, arg4,
arg5, arg6);
if (identical(arg8, NO_ARG)) return super.call(arg0, arg1, arg2, arg3, arg4,
arg5, arg6, arg7);
if (identical(arg9, NO_ARG)) return super.call(arg0, arg1, arg2, arg3, arg4,
arg5, arg6, arg7, arg8);
return super.call(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
}
*It works for 0-9 arguments - same as callsTo

"this" in a method call in TypeScript?

How do I want to make this work:
class TestClass {
doMethod1 (arg1, arg2, cb)
{
this.doMethod2(arg1, arg2, function (result){cb (result)});
}
doMethod2 (arg1, arg2, cb) {
this.doMethod3(arg1, arg2, function(result){cb (result)});
}
doMethod3 (arg1, arg2, cb) {
var result = arg1 + arg2;
cb(result);
}
}
test = new TestClass;
test.doMethod3(1,1, cb);
test.doMethod2(1,1,cb);
Both work.
test.doMethod1(1,1,cb);
EDIT: Actually, it does work.
I got around related lexical scoping issues by using the "fat arrow" syntax:
doMethod1 (arg1, arg2, cb)
{
this.doMethod2(arg1, arg2, (result) => {cb (result)});
}
Ensures that the "this" in doMethod1 is the same as the "this" in the anonymous callback function.
To preserve the lexical scoping of this in TypeScript, you use Arrow Function Expressions.
They are defined in section 4.9.2 of the TypeScript Language Specification.
ArrowFormalParameters => { return AssignmentExpression ; }
Which in code can look like:
() => { alert(this.arg1); }

Resources